web开发出现404错误,快奔溃了,求指点!!不胜感激!!
用户调查页面index.japindex.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="utf-8">
<title>问卷调查</title>
</head>
<body>
<h4>问卷调查页面</h4>
<div style="text-align: center;">
<form action="=servlet" method="post">
<table>
<tr><td>你的职业:</td><td><input type="text" name="job"></td></tr>
<tr><td>你的兴趣爱好:</td>
<td><input type="checkbox" name="hobby" value="sport">体育运动
<input type="checkbox" name="hobby" value="travel">旅游
<input type="checkbox" name="hobby" value="movie">看电影
<input type="checkbox" name="hobby" value="books">看书
<input type="checkbox" name="hobby" value="computer game">电脑游戏</td></tr>
<tr><td>你的月收入:</td>
<td><select name="salary">
<option value="5000">5000</option>
<option value="6000">6000</option>
<option value="7000">7000</option>
<option value="8000">8000</option>
<option value="9000">9000</option>
<option value="10000">10000</option>
</select>
</td></tr>
<tr><td>你工作的城市:</td>
<td><input type="radio" name="working_city" value="beishangguangshen">北上广深
<input type="radio" name="working_city" value="shenghuichengshi">省会城市
<input type="radio" name="working_city" value="other">其他
</td></tr>
<tr><td><input type="submit" name="submit" value="提交"></td>
<td><input type="reset" nmae="reset" value="重置"></td></tr>
</table>
</form>
</div>
</body>
</html>
第一个服务器,用来请求转发到第二个服务器
Servlet.java
package myServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8"); //设置服务器端编码
response.setContentType("text/html;charset=utf-8");
String job=request.getParameter("job");
String[] hobbys=request.getParameterValues("hobby");
String salary=request.getParameter("salary");
String working_city=request.getParameter("working_city");
request.setAttribute("job",job);
request.setAttribute("hobbys",hobbys);
request.setAttribute("salary",salary);
request.setAttribute("working_city",working_city);
RequestDispatcher rd=request.getRequestDispatcher("secondservlet");
rd.forward(request,response);
}
}
第二个服务器
SecondServlet.java
package myServlet;
//java.servlet包,定义了开发与协议无关的servlet的接口和类
import javax.servlet.ServletException;
//Java.servlet.annotation包:定义了注解类型和枚举类型
import javax.servlet.annotation.WebServlet;
//java.servlet.http包,定义了开发采用http协议通信的servlet的接口和类
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String TITLE="问卷调查结果";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out= response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html><head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>"+TITLE+"</title></head>");
out.println("<body><h4>"+TITLE+"</h4>");
out.println("<table>");
out.println("<tr><td>职业:</td>");
out.println("<td>"+request.getAttribute("job")+"</td></tr>");
out.println("<tr><td>兴趣爱好:</td>");
out.println("<td>");
if(request.getAttribute("hobbys")!=null) {
out.println(request.getAttribute("hobbys")+"<br/>");
}
out.println("</td></tr>");
out.println("<tr><td>月收入:</td>");
out.println("<td>"+request.getAttribute("salary")+"</td></tr>");
out.println("<tr><td>工作的城市:</td>");
out.println("<td>"+request.getAttribute("working_city")+"</td></tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>MyIdeaProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>myServlet.Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>myServlet.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/secondservlet</url-pattern>
</servlet-mapping>
</web-app>
本帖最后由 声声慢boom 于 2019-10-1 18:16 编辑
在线等回复{:10_266:}
错误显示如下:
HTTP Status 404 – 未找到
Type Status Report
消息 /MyIdeaProjects_war_exploded/=servlet
描述 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。
Apache Tomcat/9.0.26 index.jsp的页面能正常出来,但是写入数据点击提交后就出现404错误了{:10_243:} 404要不是路径错误,要不就是报错了,程序中断没有给容器返回数据。 404表示路径错误
index.jsp 的 form标签中的action="=servlet"写错了吧,应该是 action="servlet"
页:
[1]