0

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?

Usman Riaz
  • 2,920
  • 10
  • 43
  • 66

2 Answers2

2

It is simply because the render request and the post request are different requests:

  • client sends a first request, and server side a jsp is used to build a response. Request scope beans created in JSP exist only in that one
  • user fills in the form and posts a new requests

The only ways to have information to persist between requests are:

  • the session (and the flash that is provided by some frameworks like ruby on rails or Spring MVC)
  • form fields (including hidden ones)
  • URL parameters
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

For clarity , you need to take your bean declaration <jsp:useBean id="userBean" scope="request" class="com.iceman.bean.UserBean"></jsp:useBean> outside form element , and change definition to

<jsp:useBean id="userBean" scope="request" class="com.iceman.bean.UserBean">
<jsp:setProperty name="userBean" property="*" />
</jsp:useBean>

Star ( * ) means that all bean properties with names that match request parameters sent to the page are set automatically. ( As per O`Reilly book )

then you need to change line <input type="text" value="${userBean.userName}"/> to <input type="text" name="one of field names as per your UserBean class "/>

You have not shared details of UserBean class but I guess field name is userName

Regarding bean being NULL in request , please refer answer by Jan Zyka for question jsp useBean is NULL by getAttribute by servlet

Hope it helps !!

Community
  • 1
  • 1
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
  • Thanks for your kind answer @Sabir Khan. I am still not able to get bean from request in servlet. i have edited my code as per your answer. kindly take a look – Usman Riaz Dec 26 '15 at 07:07
  • your `` is outside `` tag , that needs to be inside. – Sabir Khan Dec 26 '15 at 07:10
  • It's inside now but still getting null pointer exception in servlet – Usman Riaz Dec 26 '15 at 07:14
  • try adding `<%@page import="com.iceman.bean.UserBean"%>` in your jsp – Sabir Khan Dec 26 '15 at 07:18
  • well i am using fully qualified name in class attribute, i did it anyway but error still there? can you please take a look at my servlet code above? i think there must be something missing. – Usman Riaz Dec 26 '15 at 07:22
  • if bean is found in statement `UserBean userBean = (UserBean)request.getAttribute("userBean");`, that means bean is present in request scope but not instantiated, which means some issue on jsp side. Is that the case? – Sabir Khan Dec 26 '15 at 07:31
  • I don't think so. You can always call getAttribute(name). if there is some value it will not be null. i just checked the bean if it were null. It is null. that means bean is not in request. You can call getAttribute("anyting") and cast it to bean. That does'nt matter. – Usman Riaz Dec 26 '15 at 07:39
  • I think your problem is answer by Jan Zyka for question [jsp useBean is NULL by getAttribute by servlet](http://stackoverflow.com/questions/5027160/jsp-usebean-is-null-by-getattribute-by-servlet). Also we do get "bean [name] not found within scope" message is bean is not present, not sure if your case is any different. – Sabir Khan Dec 26 '15 at 07:40