本文主要是介绍servlet表单验证404错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
做登录的小练习的时候,登录跳转出现了404错误,源码方面检查了半天,无果。最后发现是eclipse的问题,换了一个eclipse后成功运行,贼尴尬。
说一下网上的解决流程:
1、查看form标签的action路径是否正确
2、查看servlet的包名和web.xml里servlet-class标签的全类名是否一致
3、查看web.xml里url-pattern标签的映射是否正确
以上都是路径问题。
我的错误在于运行项目后eclipse配置的tomcat奔腾不止,服务器一直无法创建。
解决办法:
1)重新配置eclipse(烦)
2)扔了现在的eclipse,再找一个eclipse配置环境运行项目。(我这么干的)
自己没有eclipse的小伙伴可以官网下载,找好基友copy,或者找我要
以下是代码:(没什么卵用,可以不看)
登录页代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body><span>hello world</span><form action="/login/LoginServlet" method="get"><div>用户名:</div><input type="text" id="name" name="name"></input><div>密码:</div><input type="text" id="pwd" name="pwd"></input><input type="submit" value="登录"><a href="register.html">注册页>></a></form>
</body>
</html>
servlet页代码:
package com.exam.servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** Servlet implementation class LoginServlet*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8"); String loginName=request.getParameter("name");String loginPwd=request.getParameter("pwd");if (loginName.equals("admin")&&loginPwd.equals("123")){System.out.println("登录成功");}else{System.out.println("登录失败");}request.getRequestDispatcher("register.html").forward(request,response);}}
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_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>login</display-name><servlet><servlet-name>LoginServlet</servlet-name><servlet-class>com.exam.servlet.LoginServlet</servlet-class> </servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/LoginServlet</url-pattern></servlet-mapping><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>
</web-app>
这篇关于servlet表单验证404错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!