I have a jsp page where i am using a bean as request scope bean. I have an input text whose value is the attribute of the bean. On the form submit my action is someServlet. Now in that someServlet i want to access the bean that i used in my jsp page. here is sample code i am using.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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="userBean" scope="request" class="com.iceman.bean.UserBean">
<jsp:setProperty property="*" name="userBean"/></jsp:useBean>
<form action="action.do" method="post">
Type Your Name:<input type="text" name="userName"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Servler
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
UserBean userBean = (UserBean)request.getAttribute("userBean");
response.getWriter().print(userBean.getUserName());
}
When i run this i get a null pointer exception on servlet line where i call bean getter method.
Where am i doing wrong?