1

I am working on one simple JSP-Servlet-Hibernate Application.I have successfully retrieved data using Hibernate. I am facing problem while set up the same value (retrieved from Servlet) on jsp page.

I have tried with Expression Language. but it does not working. I dont know why it is not working.

Servlet Code::

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        User user=new User();
        user.setUserId(Integer.parseInt(request.getParameter("Id")));
        user.setUserName(request.getParameter("name"));
        user.setUserEmail(request.getParameter("email"));
        user.setUserPhone(request.getParameter("number"));
        HibernateTest ht=new HibernateTest();
        User user2= ht.saveDetails(user);
        //fileRead();
        RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
        rd.include(request, response);
        request.setAttribute("user", user2);
        System.out.println(user2.getUserId());
        response.setContentType("text/html");
        PrintWriter pw=response.getWriter();
        pw.println("Saved successful and retrieve successful..............."+ ((User)request.getAttribute("user")).getUserId());
    }

jsp page::

<form action="FirstServlet" method="post">
<table border="1">
<tr>
<td>User Id:</td>
<td><input type="text" name="Id" value="${reqestScope.user.userId}"/></td><!-- it remains blank at retrieval time -->
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" name="name" value="${reqestScope.user.userName}" /></td><!-- it remains blank at retrieval time -->
</tr>
<tr>
<td>User Email:</td>
<td><input type="text" name="email" value="${reqestScope.user.userEmail}"/></td><!-- it remains blank at retrieval time -->
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="number" value="${reqestScope.user.userPhone}"/></td><!-- it remains blank at retrieval time -->
</tr>
<tr><td colspan="2"><input type="submit" value="Submit"/></tr>
</table>
</form>
<% 
if(((User)request.getAttribute("user"))!=null)
    System.out.println(((User)request.getAttribute("user")).getUserId()); %><!-- it prints the correct result.. -->

Values retrieved from Servlet class should be displayed properly on jsp page. but actually, it remains blank at the time of retrieval.

Ajay
  • 13
  • 4
  • The controller in the server side doesn't return anything, it's set to void. Better use a different controller to initialize your form and another one to post the form after the user has filled it. – Christos Karapapas Apr 28 '19 at 12:10
  • I have used servlet as controller and i am inserting and fetching the same record from database using hibernate and then i have set the response of hibernate as a "user" attribute of request. – Ajay Apr 28 '19 at 14:54
  • here `${reqestScope.user.userId}` , it should be `${requestScope.user.UserId}` ,i.e u needs to be capital `U` do same for other fields and see once. – Swati Apr 28 '19 at 15:11
  • As per my model class, i have declared it as , public class User { @Id private int userId; private String userName; private String userEmail; private String userPhone; // getters and setters } that's why i have put u as lowercase for all fields. – Ajay Apr 28 '19 at 15:19
  • did you import `jstl` library? – Swati Apr 28 '19 at 15:53
  • No. how we can do the same by jstl library? – Ajay Apr 28 '19 at 16:03
  • add these line at top `<%@page contentType="text/html;charset=UTF-8" language="java" %> <%@page isELIgnored="false" %>` of your jsp page and see if it works – Swati Apr 28 '19 at 16:57
  • @swati , still it doesn't work :( – Ajay Apr 28 '19 at 17:13
  • Maybe [this](https://stackoverflow.com/questions/793983/jsp-el-expression-is-not-evaluated) might help you. – Swati Apr 28 '19 at 18:26

1 Answers1

0

you should try to right correctly requestScope (you wrote reqest) and if it doesn't work, you try it using JSTL, so instead of :

<input type="text" name="Id" value="${requestScope.user.userId}"/>

you should try :

<input type="text" name="Id" value="<c:out value="${requestScope.user.userId}"/>" >
Big Zed
  • 417
  • 5
  • 11
  • try to put `action="/FirstServlet"` instead of `action="FirstServlet"`. if it is still not working remove the attribute `action` from `
    `.
    – Big Zed Apr 29 '19 at 19:21
  • Servlet is called by this code and i also get the desired output at jsp page as, if(((User)request.getAttribute("user"))!=null) System.out.println(((User)request.getAttribute("user")).getUserId()); %> The only problem is, i want to fill the values which is there in request attribute and to achieve the same i have used Expression Language but i am unable to set the values at jsp page text box. – Ajay Apr 30 '19 at 03:32