我在前向方法调度程序中收到错误。 这是我的示例代码,我收到错误消息。我需要使用s2.java文件显示名称。
它显示了html页面,我把名字放在第一位和最后一位,然后提交它显示错误。
/*this is s1.java(first java file)*/
package rqdis;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class S1 extends HttpServlet{
public void doPost(HttpServletResponse rs,HttpServletRequest rq)
throws ServletException,IOException{
try{
rs.setContentType("text/html");
PrintWriter p=rs.getWriter();
String s1=rq.getParameter("n1");
String s2=rq.getParameter("n2");
p.print(s1+" "+s2);
RequestDispatcher rd=rq.getRequestDispatcher("/fff");
rd.forward(rq, rs);
p.print("am from server 1.!");
p.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
/*this is s2.java(second java file)*/
package rqdis;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class s2 extends HttpServlet{
public void doPost(HttpServletRequest rq,HttpServletResponse rs)
throws ServletException,IOException{
try{
rs.setContentType("text/html");
PrintWriter p=rs.getWriter();
String s1=rq.getParameter("n1");
String s2=rq.getParameter("n2");
p.print(s1+" "+s2);
p.print("am from server 2.!");
p.close();
}catch (Exception e){
e.printStackTrace();
}
}
}

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>rqdis</display-name>
<welcome-file-list>
<welcome-file>main.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>firstservlet</servlet-name>
<servlet-class>rqdis.S1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstservlet</servlet-name>
<url-pattern>/ff</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>secondservlet</servlet-name>
<servlet-class>rqdis.s2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>secondservlet</servlet-name>
<url-pattern>/fff</url-pattern>
</servlet-mapping>
</web-app>
&#13;
<html>
<head>
<meta charset="ISO-8859-1">
<title>disfor</title>
</head>
<body>
<form action="forward metod" method="post">
First name:
<input type="text" name="n1"> Last name:
<input type="text" name="n2">
<input type="submit" value="Submit">
</form>
</body>
</html>
&#13;
答案 0 :(得分:1)
当您转发到另一个servlet时,不应该在servlet中写入,因为响应将在另一个servlet中提交。
答案 1 :(得分:0)
正如我从代码中看到的那样,您将获得的错误是404(未找到)。因为您的表单操作是forward metod
在web.xml文件中没有url映射。表单操作用于指定应在何处进行请求的URL。
尝试将表单操作更改为action="/ff"
。