TerryPhan TerryPhan - 7 months ago 22
Java Question

jsp bean's Scope Request doesn't work

I have a problem with JSP Bean's scope - Request. I have a page Index.jsp with jsp bean 'message', its scope is Request and a page result.jsp. When I send request to result.jsp from Index.jsp. My bean 'message' should keep its value but it doesn't now.

I tried with scope Session and my bean worked well. I search all questions about this problem but no answer can meet my question.

Here is my code:
file Index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
</head>
<body>

<%
String name = request.getParameter("name") == null ? "" :
request.getParameter("name");
int age = ( request.getParameter("age") == null ||
request.getParameter("age") == "") ? 0 :
Integer.parseInt(request.getParameter("age"));
%>

<h1>Nice to meet you</h1>

<form method="post" action="View/result.jsp">
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:setProperty name="message" property="message" value="Hello world!"/>

<label>Name: </label> <br>
<input type="text" name="name" placeholder = "Phan Dinh The"/> <br>

<label>Age: </label> <br>
<input type="number" name="age" placeholder = "25"/> <br>

<input type="checkbox" name="title"/> Senior <br>

<input type="radio" name="language" value="c#"/> C# <br>
<input type="radio" name="language" value="java"/> Java <br>

<br><br>

<jsp:include page="View/date.jsp" flush="true"/>


<input type="submit" value="submit"/>
<br><br>


</form>

<br><br>
</body>
</html>


file result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.java.Message"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:getProperty name="message" property="message"/>
</body>
</html>


my class Message

package com.java;

public class Message {

private String message;

public String getMessage() {
return message;
}

public void setMessage(String content) {
this.message = content;
}
}


I use Tomcat 8.0.23, Jsp version 2.3, Servlet API 3.1

Answer

When you use

<jsp:setProperty name="message" property="message" value="Hello world!"/>

in the index.jsp file, that property is scoped to the request of the index.jsp page. once the index jsp page returns to the client, that request is done. When you submit the form, a new request is created, and that is used for the result page generation. Thus when you are in the result.jsp code, there is no request scoped parameter named 'message'.

You could always put the message in an

<input type="hidden" name="message">Hello World</input>

field of the form, and retrieve it in the results.jsp that way.

Comments