为什么servlet跳转报错404
ServerRedirectDemo.javaimportjavax.servlet.*;importjavax.servlet.http.*;publicclassSe...
ServerRedirectDemo.java
import javax.servlet.*;
import javax.servlet.http.*;
public class ServerRedirectDemo {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
req.getSession().setAttribute("name", "测试");
req.setAttribute("info", "SZKEDUN");
RequestDispatcher rd = req.getRequestDispatcher("get_info.jsp");
rd.forward(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
this.doGet(req, resp);
}
}
web.xml
<servlet>
<servlet-name>server</servlet-name>
<servlet-class>kedun.dhy.ServerRedirectDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>server</servlet-name>
<url-pattern>/ServerRedirectDemo</url-pattern>
</servlet-mapping>
还是报错404
get_info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>get_info.jsp</title>
</head>
<% request.setCharacterEncoding("UTF-8"); %>
<body>
<h2>属性:<%=session.getAttribute("name")%></h2>
<h2>名字:<%=request.getAttribute("info")%></h2>
</body>
</html> 展开
import javax.servlet.*;
import javax.servlet.http.*;
public class ServerRedirectDemo {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
req.getSession().setAttribute("name", "测试");
req.setAttribute("info", "SZKEDUN");
RequestDispatcher rd = req.getRequestDispatcher("get_info.jsp");
rd.forward(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
this.doGet(req, resp);
}
}
web.xml
<servlet>
<servlet-name>server</servlet-name>
<servlet-class>kedun.dhy.ServerRedirectDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>server</servlet-name>
<url-pattern>/ServerRedirectDemo</url-pattern>
</servlet-mapping>
还是报错404
get_info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>get_info.jsp</title>
</head>
<% request.setCharacterEncoding("UTF-8"); %>
<body>
<h2>属性:<%=session.getAttribute("name")%></h2>
<h2>名字:<%=request.getAttribute("info")%></h2>
</body>
</html> 展开
展开全部
正如楼上所说的,你没有继承,下面才是servlet,而且使用工具(eclipse或者myeclipse)创建servlet时,会在web。xml中生成映射,你如果只是copy的别人的servlet,还需要去web.xml里面配置
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
展开全部
404本身是请求不到应答。
这类错误一般都是请求地址对应不正确或者地址中由于自己拼接连部分接符中的缺失导致的。这类错误没有源码且不知道地址情况并不能真正意义上的帮你解决问题。
你是到了后台处理跳转其他时报错(forword时报错)还是直接访问后台doGet/doPost时报错?
如果是前者那么在后台代码的地址单独system.out.print出来访问看看,如果是后者只能反复确认你的地址完整性,正确性。
还有极个别情况是缓存未清理,jar包冲突导致。
这类错误一般都是请求地址对应不正确或者地址中由于自己拼接连部分接符中的缺失导致的。这类错误没有源码且不知道地址情况并不能真正意义上的帮你解决问题。
你是到了后台处理跳转其他时报错(forword时报错)还是直接访问后台doGet/doPost时报错?
如果是前者那么在后台代码的地址单独system.out.print出来访问看看,如果是后者只能反复确认你的地址完整性,正确性。
还有极个别情况是缓存未清理,jar包冲突导致。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ServerRedirectDemo这个类需要继承HttpServlet。不然就不是一个servlet了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询