Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing request attributes to jsp

843840Feb 27 2008 — edited Feb 27 2008
Hello,

I try to build a simple webApp, and got some strange problems. I have a JSP with a "submit" button page and a simple servlet, that passes a message back to page. However, the message does not appear on the page. There is the code for JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- It is a debug code. It shows that request attributes indeed contain the message -->
Attributes:<br/>
<% java.util.Enumeration attrNames = request.getAttributeNames();
	while(attrNames.hasMoreElements()){
		String key = (String)attrNames.nextElement();
%>
<%=key %>=<%=request.getAttribute(key) %><br/>
<%
	}
%>
<br/>
Parameters:<br/>
<% java.util.Map paramMap = request.getParameterMap();
	for(Object key : paramMap.keySet()){
		String[] vals = (String[])paramMap.get(key);
		for(int i = 0, size = vals.length; i<size; i++){
%>
<%=key %>=<%= vals%>
<%
}
}
%>

<!-- end of debug code -->
Body:

<jsp:useBean id="messageBean" class="testpackage.MessageBean" scope="request">
<jsp:setProperty name="messageBean" property="message"/>
</jsp:useBean>
<jsp:getProperty name="messageBean" property="message"/>

<form action="/testWebApp/TestServlet">
<input type="submit"/>
<input type="hidden" name="requestParam" value="valueOfRequestParam"/>
</form>
</body>
</html>
and the code for servlet:
package testpackage;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

public TestServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("message", "It works from GET!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("message", "It works from POST!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
and for the message bean I use in JSP:
package testpackage;

public class MessageBean {
String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
I can't figure out why the "message" property of the messageBean in the JSP is not filled from the request. Any help would be very appreciated!

Update: ok, I've figured out that only parameters could be handled by JSP bean, and that is why the bean property didn't  filled in. So now my question is - is it possible to handle request attributes(not parameters) with a standard JSP syntax (not using scriptlets)?

Edited by: TimurF on Feb 27, 2008 2:44 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2008
Added on Feb 27 2008
1 comment
270 views