我正在开发一个简单的JSP-Servlet-Hibernate应用程序。我已经成功使用Hibernate检索了数据。在jsp页面上设置相同的值(从Servlet检索)时,我遇到了问题。
我尝试过表达语言。但它不起作用。我不知道为什么它不起作用。
Servlet代码::
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页面:
<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.. -->
从Servlet类检索的值应正确显示在jsp页面上。 但实际上,在检索时它仍然是空白的。
答案 0 :(得分:0)
您应该尝试正确纠正requestScope
(您写了reqest
),如果它不起作用,请使用JSTL进行尝试,所以不要使用:
<input type="text" name="Id" value="${requestScope.user.userId}"/>
您应该尝试:
<input type="text" name="Id" value="<c:out value="${requestScope.user.userId}"/>" >