为了账号安全,请及时绑定邮箱和手机立即绑定

servlet实现登陆出现404,感觉没错是不是哪里潜在的原因

servlet实现登陆出现404,感觉没错是不是哪里潜在的原因

慕先生4463397 2017-12-11 22:27:45
package com.tz.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;@WebServlet("Login")public class Login extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 1L;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        doPost(req,resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        String username=req.getParameter("username");        String password=req.getParameter("password");        if(username!=null&&password!=null&&username.equals("aaa")&&password.equals("1234")){            req.setAttribute("username", username);            req.getRequestDispatcher("/welcome").forward(req,resp);        }else{            req.getRequestDispatcher("/error").forward(req,resp);        }    }}package com.tz.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/welcome")public class Welcome extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 1L;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        // TODO Auto-generated method stub        super.doGet(req, resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        String username=req.getParameter("username");        PrintWriter out = resp.getWriter();        out.println("<html>");            out.println("<head>");               out.println("<title>登录信息</title>");                out.println("</head>");              out.println("<body algin=center>");               out.println("<h2>"+"歡迎您"+username+"</h2>");              out.println("</body>");              out.println("</html>");    }}/** *  */package com.tz.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author Administrator * */@WebServlet("/error")public class Error extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 1L;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        doPost(req,resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        PrintWriter out =resp.getWriter();            out.println("<html>");                out.println("<head>");                   out.println("<title>登录信息</title>");                    out.println("</head>");                  out.println("<body algin=center>");                   out.println("<h2>登陆错误请重新登陆</h2>");                  out.println("</body>");                  out.println("</html>");    }}<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><style type="text/css">    #box{width:500px;height:500px;margin:100px auto;}    #sub{text-align:center}    #box form div{width:230px;height:40px;line-heihgt:340px}</style></head><body>    <div id="box">        <h3>聊天室</h3>        <form action="/land/Login" method="post">            <div class="text">账号<input type="text" name="username"  /></div>            <div class="pas">密码<input type="password" name="password"  /></div>            <div class="sub"><input type="submit" value="登陆" class="sub"></div>        </form>    </div></body></html>
查看完整描述

1 回答

?
产品经理不是经理

TA贡献481条经验 获得超143个赞

  1. 第一个Servlet注解改为:@WebServlet("/Login");

  2. 表单form里的 action="/land/Login"改为action="${pageContext.request.contextPath}/Login"

  3. 使用@WebServlet注解的话,就不用在web.xml里面配置Servlet与UrlPattern了。

查看完整回答
反对 回复 2017-12-12
  • 1 回答
  • 0 关注
  • 1218 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信