JSP调用servlet无法正常工作

时间:2016-02-15 07:54:12

标签: jsp servlets

我有一个JSP页面,其中包含一个表单,我必须从中调用一个servlet。但即使在web.xml中添加servlet映射并尝试webservlet注释之后,JSP页面本身也没有加载。我很困惑该怎么做。

JSP页面:

<html>
   <h1>Active Users </h1>
   <body>
      <form id="userForm" action="${pageContext.request.contextPath}/users" method="post">
        <ul id="list" style="list-style: none;">
           <input type="submit" name="delete" value="Delete"/>
           <input type="reset" name="cancel" value="Cancel"/>
        </ul>
      </form>
   </body>
</html>

调用servlet:

//@WebServlet(name = "users", urlPatterns = ("/users"))
public class UserManagementControllerServlet extends HttpServlet
{

  private static final long serialVersionUID = 1L;

  /**
  * @see HttpServlet#HttpServlet()
  */
  public UserManagementControllerServlet() {
    super();

  }  
  public void init() throws ServletException{
           Logger.getLogger(UserManagementControllerServlet.class).info("UserManagementControllerServlet initiated");
}

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {
    try{
        //to do
        response.sendRedirect("users.jsp");
    }catch(Exception e){

    }
    System.out.println("Reached here!!");

 }
}

这是我的web.xml配置:

<servlet>
  <servlet-name>UserManagementControllerServlet</servlet-name>
  <servlet-class>com.ehc.plugins.UserManagementPlugin.UserManagementControllerServlet</servlet-class>
  <jsp-file>/jsp/users.jsp</jsp-file>
</servlet>

<servlet-mapping>
  <servlet-name>UserManagementControllerServlet</servlet-name>
  <url-pattern>/users</url-pattern>
</servlet-mapping>

2 个答案:

答案 0 :(得分:1)

您不能在web.xml中的同一个servlet标记中使用servlet-class和jsp-file。见http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html#1039287

答案 1 :(得分:0)

除了您的JSP页面位于/jsp/users.jsp之外,您还应该可以通过GET方法访问它。

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws Exception  {
try{
    //process the request
    ...
    req.getRequestDispatcher("/jsp/users.jsp").forward(req, resp);

}catch(Exception e){
  e.printStackTrace();
  throw e;
}
}

当POST方法从表单获取数据时,处理它并重定向到servlet。如果它是相同的servlet

@Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception 
 {
    try{

         Strind data = request.getParameter("delete");
        //todo
         ...
        response.sendRedirect(request.getContextPath()+"/users");
    }catch(Exception e){
      e.printStackTrace();
      throw e;
    }
    System.out.println("Reached here!!");
}

要在servlet中使用注释,您应该使用支持版本&gt; = 3.0的servlet容器,或使用web.xml明确定义版本

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <description>Users Web Application</description>
  <display-name>Users</display-name>

<servlet>
  <servlet-name>UserManagementControllerServlet</servlet-name>
  <servlet-class>com.ehc.plugins.UserManagementPlugin.UserManagementControllerServlet</servlet-class>  
</servlet>

<servlet-mapping>
  <servlet-name>UserManagementControllerServlet</servlet-name>
  <url-pattern>/users</url-pattern>
</servlet-mapping>
</web-app>