Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help with JSP & Servlet

 
Randall Stevens
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very simple JSP that I am trying to get to work. But first a little history:
I have an HTML page that POSTs to my server. A servlet takes the data and performs several functions on the values and does a response.setAttribute(myAttribute,value); then does a reponse.getRequestDispatcher("webfiles/my.jsp")
my.jsp is very generic
<html>
<head>
</head>
<body>
<FORM ACTION="/examples/servlet/AnotherServlet" ENCTYPE=
"multipart/form-data" >
<% String myAttrib = request.getParameter(myAttribute) %>
<input type="text" name="TextInput" size=6 maxlength=6>
<input type="submit" value="submit" >
</body>
</html>
The problems I am having are:
1. How do I know that the attribute is being set?
2. Am I using the getRequestDispatcher correctly?
3. How do I pass back both the TextInput parameter and the myAttrib parameter?
It has been three years since I have done this, I do not seem to remember everything I need to get it correctly functioning.
Randall
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64711
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I know that the attribute is being set?

Within your current JSP you don't. The getParameter() method is used to retrieve submitted request parameters (form submission or query string). What you want is getAttribute().
Am I using the getRequestDispatcher correctly?

Assuming the URL is correct, half correctly. After creating the dispatcher you need to forward to the JSP page.
How do I pass back both the TextInput parameter and the myAttrib parameter?

The TextInput will become part of the form submission data (retrieved in the next action by getParameter()). To also cause the myAttrib value to be passed along, generate a hidden input with that data in the form.
bear
[ October 09, 2003: Message edited by: Bear Bibeault ]
 
Randall Stevens
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. How do I get the relative path for the url to be correct? I am using a default configuration of Tomcat 4.0.4, the servlet is in the 'webapps/web-inf/classes' directory, the jsp is in 'webapps/root' directory.
2. Can you show me the correct usage of response.getAttribute() in my example?
In using the 'Core Web Programming, Second Edition' by Marty Hall and Larry Brown, the example they are using to retrieve attributes is <jsp:useBean id="key" scope="session"> . I am not sure if that is correct for my situation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic