tags:

views:

149

answers:

2

Hi,

I currently have a JSP page with a Form for the user to enter their name, but what I want is to get the user forwarded to a different JSP page after form submission and to carry on their name to be used.

I don't want to use JSTL EL just simple JSP uses.

I was thinking of using a bean storing the detail in a session but how would it work.

Thanks.

+2  A: 

You'd have JSP enter the info into a form and POST it to a servlet. The servlet would validate the form input, instantiate the bean, add it to session, and redirect the response to the second JSP to display.

You need a servlet in-between. JSPs using JSTL are for display; using the servlet this way is called MVC 2. Another way to think of it is the front controller pattern, where a single servlet handles all mapped requests and simply routes them to controllers/handlers.

duffymo
O Ok I understand what you mean, get a controller to act as a bridge between the two? Would you able to point to any snippets online or anything which can help me get started? Thanks
Sandeep Bansal
I would love to post an extended answer or point some answers I posted before with code samples, but not before you elaborate more about your aversion against JSTL/EL.
BalusC
+1  A: 

duffymo you have the best idea but here is a quick solution by just passing things through the JSP. Here is the JSP with the form

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body><form name="test" action="./stackTest2.jsp" method="POST">
 Text Field<input type="text" name="textField">
  <input type="submit"> 
</form> </body>
</html>

and then the second page looks like this:

<html>
<head><title>Simple jsp page</title></head>
<body><%=request.getParameter("textField")%></body>
</html>

And then put information in a hidden field, you can get information by using the request.getParameter method. This just prints out what was in the form, but using the same idea for inputting it in to the hidden field in a form. I recommend this as all my experience with sessions have ended in a failure. I STRONGLY DO NOT Recommend this method, MVC is a much better way of developing things.
Dean

Dean
Thanks, short and sweet
Sandeep Bansal