Reputation: 1253
Friends, I am new to JSP technology; I am having a problem in setting beans property using <jsp:UseBean>
tag for request scope. What I am trying to do is that I am setting the beans property in JSP page using <jsp:UseBean>
with scope request and on <form>
submit button I am hitting servlet class to check if the bean was added to request scope or not. I am unable to add my bean to request scope. However when change the scope to session, then there is no problem.
why is it so. Can anyone guide me.?
bean class
package mypack;
public class Car {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
index.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>
<jsp:useBean id="car" class="mypack.Car" scope="request">
<jsp:setProperty name="car" property="id" value="111"></jsp:setProperty>
<jsp:setProperty name="car" property="name" value="Jeep"></jsp:setProperty>
</jsp:useBean>
<form method="post" action="CarServlet">
<input type="submit" value="submit">
</form>
</body>
</html>
CarServlet
package mypack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/CarServlet")
public class CarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CarServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
//HttpSession session=request.getSession();
Car c=(Car)request.getAttribute("car"); // if I change request to session problem solves...
out.println("........"+c.getName());
}
}
Output on webpage
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
mypack.CarServlet.doPost(CarServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.
Apache Tomcat/7.0.34
Upvotes: 1
Views: 3594
Reputation: 449
Name was not previously introduced" indicates that you haven't told your JSP about this bean as of yet.
You need to add
<jsp:useBean id="taskBean" class="com.mybean.TaskBean" scope="request" />
before you access the properties using jsp:getProperty
Also,
The "name" attribute of jsp:getProperty should match the "id" attribute of the declared bean.
Then access
<jsp:getProperty name="taskBean" property="multiplier" />
With Scriptlet When you use
TaskBean taskBean = (TaskBean) request.getAttribute("taskBean");
Check You have already added
<%@page import="com.mybean.TaskBean"%>
statemement in your JSP.
So, When JSP page gets compiled like RequestScope_jsp.java.
First, It has import statement for your bean Second, _jspService method has request and response parameters as argument.
_jspService(HttpServletRequest request, HttpServletResponse response)
Thus, you can forwarded JSP pages access them using scriptlets. The same things happens with EL.
Remember thumb rule
: When you access bean property using jsp:getProperty, you require to add jsp:useBean which is used to locate and instantiate a bean class
Upvotes: 4