Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Regex In Jsp With Beans!

JSP beans

  • Please log in to reply
3 replies to this topic

#1 nick3

nick3

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 20 February 2009 - 05:51 AM

Hello!
I have a small problem with my JSP-code, im "playing" to learn abit so the program dont really do much but still... I want to get a regex to check the input in my form, but dont know how i can do that when im using beans in JSP.

Here is my first codepart (index.jsp)

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Submit!</title>
        <link type="text/css" href="Style.css" rel="stylesheet"/>
    <script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-17086927-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script></head>
    <body>
    <logo><img src="logo.jpg" width="459" height="152" alt="logo"/></logo>

<div1>
<form method=POST action="submit.jsp">
    <label>Item:</label> <input type="text" name="item" size="20"<BR>
    <label>Price:</label> <input type="text" name="price" size="20"<BR>
    <label>Stock:</label> <input type="text" name="stock" size="20"<BR>
<input type="submit" value="Lägg in item">
</form>
</div1>

</body>
</html>

Here is the page i send the form to and where the bean gets the form and sends it to my Java-class (next codepart)

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@page import="item.Items" %> 
<jsp:useBean id="item" scope="page" class="item.Items" />
<jsp:setProperty name="item" property="*" />


<% if(session.getAttribute("Array") == null) {
    ArrayList<Items> arr = new ArrayList<Items>();
    arr.add(item);
    session.setAttribute("Array", arr);
}
   else {
    ArrayList<Items> ar2 = (ArrayList<Items>) session.getAttribute("Array");
    ar2.add(item);
    session.removeAttribute("Array");
    session.setAttribute("Array", ar2);
   }  
response.sendRedirect("view.jsp");    
%>

Here is my Javaclass...

public class Items {
    private String item;
    private int price, stock;

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public void setItem( String value )
    {
        item = value;
    }

    public void setPrice( int value )
    {
        price = value;
    }

    public String getItem(){

        return item;
    }

    public int getPrice(){

        return price;
    }

}

Now to my question, where and how do i put my Regex so the user can get a errormsg in index if he types anything wrong? And lets him correct it. I would love an example in my own code so i know how to do.

/Nick3
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 February 2009 - 06:49 AM

You would need to do that with JavaScript that is triggered when the user tries to submit the form.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 nick3

nick3

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 20 February 2009 - 06:52 AM

Hmm ok, i dont know any javascript really, if its not to complicated, like to show me how to do it?
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 February 2009 - 07:00 AM

I would recommend reading up on javascript at W3Schools Online Web Tutorials . There is a LOT you can do with it, and the details of what you want will affect the code you use.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: JSP, beans

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download