web06-PanduanLogin

电影网站:www.aikan66.com 

项目网站:www.aikan66.com 
游戏网站:www.aikan66.com 
图片网站:www.aikan66.com 
书籍网站:www.aikan66.com 
学习网站:www.aikan66.com 
Java网站:www.aikan66.com 
iOS网站:www.aikan66.com

----

新建web项目,名字web06-PanduanLogin

servlet PortalServlet中,判断访问用户是否已经登录,如果没有,将请求转发给LoginServlet2;

如果已经登录,则调用RequestDispacher接口的forward()方法,将请求转发给WelcomeYou,显示欢迎。

新建servlet,名字PortalServlet.java,

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        //接收前台传过来的参数user和password
        String name = request.getParameter("user");
        String pwd = request.getParameter("password");
        
        
        if("ultra".equals(name)&&"ultra".equals(pwd))
        {
            //如果已经登录,则调用RequestDispatcher接口的forward()方法,将请求转发给Welcome
            ServletContext context=getServletContext();
            RequestDispatcher rd=context.getRequestDispatcher("/welcome");
            rd.forward(request, response);
        }else{
            //如果没有登录,调用RequestDispatcher接口的include()方法,将请求转发给login2
            RequestDispatcher rd = request.getRequestDispatcher("login2");
            rd.include(request, response);
        }
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

----

新建servlet,名字LoginServlet2

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<form method=post action=portal>");
        out.println("<talbe");
        out.println("<tr>");
        out.println("<td>请输入用户名:</td>");
        out.println("<td><input type=text name=user></td>");
        out.println("</tr>");
        
        out.println("<tr>");
        out.println("<td>请输入密码:</td>");
        out.println("<td><input type=password name=password></td>");
        out.println("</tr>");
        
        out.println("<tr>");
        out.println("<td><input type=reset value=重填></td>");
        out.println("<td><input type=submit value=登录></td>");
        out.println("</tr>");
        
        out.println("</table>");
        out.print("</form>");
        
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request,response);
    }

----

新建servlet,名字Welcome

public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String user=req.getParameter("user");//获取到前台的name:user文本框中的值;前台过来的叫请求req。
        String welcomeInfo="Welcome you,"+user;//字符串的拼接
        
        resp.setContentType("text/html");
        
        PrintWriter out=resp.getWriter();
        out.println(welcomeInfo);
        out.close();
        
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("dopost");
        
        this.doGet(req, resp);
    }

----

配置web.xml

  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>PortalServlet</servlet-name>
    <servlet-class>PortalServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet2</servlet-name>
    <servlet-class>LoginServlet2</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>welcome</servlet-name>
    <servlet-class>WelcomeYou</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>welcome</servlet-name>
    <url-pattern>/welcome</url-pattern>
</servlet-mapping>
  <servlet-mapping>
    <servlet-name>PortalServlet</servlet-name>
    <url-pattern>/portal</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LoginServlet2</servlet-name>
    <url-pattern>/login2</url-pattern>
  </servlet-mapping>

----

浏览器中访问 http://localhost:8080/web06-PanduanLogin/portal

访问结果

如果已经登录

如果没有登录

原文地址:https://www.cnblogs.com/zhaixing/p/5681407.html