从jsp调用时,Servlet不会调用

时间:2014-07-09 01:26:28

标签: java jsp servlets

我无法调用我的servlet请帮帮我。以下是我的代码。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action= "/RoleCommand.do" method = "post">
name:<input type ="text" name = "name"/><br></br>
id:<input type = "text" name= "id"/><br></br>
<input type = "submit">
</form>
</body>
</html>


Servlet code


public class RoleCommand extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleCommand() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub



    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter p = response.getWriter();
        p.println("hi");
        response.sendRedirect("ValidServlet");
String name = request.getParameter("name");
String id = request.getParameter("id");
}
}

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Form</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Controller</display-name>
    <servlet-name>Controller</servlet-name>
    <servlet-class>com.controller.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/Controller</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>DatabaseCon</display-name>
    <servlet-name>DatabaseCon</servlet-name>
    <servlet-class>com.database.DatabaseCon</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DatabaseCon</servlet-name>
    <url-pattern>/DatabaseCon</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>Transaction</display-name>
    <servlet-name>Transaction</servlet-name>
    <servlet-class>com.database.Transaction</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Transaction</servlet-name>
    <url-pattern>/Transaction</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>RoleCommand</display-name>
    <servlet-name>RoleCommand</servlet-name>
    <servlet-class>com.command.RoleCommand</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RoleCommand</servlet-name>
    <url-pattern>/RoleCommand</url-pattern>
  </servlet-mapping>
</web-app>

根据上面的代码,它应该在执行后打印到控制台。但是,当我点击提交按钮时,它会抛出未找到的异常资源。

请帮帮我。

3 个答案:

答案 0 :(得分:0)

从动作和动作类中移除.do应该像以下动作= “/ RoleCommand”

答案 1 :(得分:0)

你有错误的servlet映射 在web.xml中

  <servlet>
    <description></description>
    <display-name>RoleCommand</display-name>
    <servlet-name>RoleCommand</servlet-name>
    <servlet-class>com.command.RoleCommand</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RoleCommand</servlet-name>
    <url-pattern>/RoleCommand</url-pattern>
  </servlet-mapping>

将url-pattern更改为* .do或/RoleCommand.do 或者您可以更改表单操作

<form action= "/RoleCommand.do" method = "post">

<form action= "/RoleCommand" method = "post">

注意:表单操作和url-pattern必须匹配才能从jsp调用servlet。

答案 2 :(得分:0)

按如下方式更改form,这可能会对您有所帮助

<form action="http://localhost:8080/packagename/serveltclassname" >