zoukankan      html  css  js  c++  java
  • 一个简单的jsp+servlet实例,实现简单的登录

    开发环境myeclipse+tomcat6

    1、先创建web project,项目名为RegisterSystem,

    2、在WebRoot 目录下创建login.jsp文件,只需修改body中的内容,如下所示:

    <body>
        <form action="login">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="pwd"><br>
        <input type="submit">
        </form>
      </body>

    3、在scr目录下的com.ht.servlet编写AcountBean.java文件,代码如下:

    package com.ht.servlet;

    public class AccountBean {
     private String username = "";
     private String password = "";
     public String getPassword() {
      return password;
     }
     public void setPassword(String password) {
      this.password = password;
     }
     public String getUsername() {
      return username;
     }
     public void setUsername(String username) {
      this.username = username;
     }
    }

    4、在scr目录下的com.ht.servlet编写servlet类CheckAccount.java文件,代码如下:

    package com.ht.servlet;

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class CheckAccount extends HttpServlet {

     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
      doGet(req,resp);
     }

     @Override
     public void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
      HttpSession session = req.getSession();
      AccountBean account = new AccountBean();
      String username = req.getParameter("username");
      String pwd = req.getParameter("pwd");
      account.setPassword(pwd);
      account.setUsername(username);
      if((username != null)&&(username.trim().equals("jsp"))) {
       if((pwd != null)&&(pwd.trim().equals("1"))) {
        System.out.println("success");
        session.setAttribute("account", account);
        String login_suc = "success.jsp";
        resp.sendRedirect(login_suc);
        return;
       }
      }
      String login_fail = "fail.jsp";
      resp.sendRedirect(login_fail);
      return;
     }
     
    }
    5、在WebRoot目录下编写success.jsp文件 成功后跳转

    <body>
        <%
        AccountBean account = (AccountBean)session.getAttribute("account");
        %>
        username:<%= account.getUsername()%>
         <br>
         password:<%= account.getPassword() %>
      </body>

    6、在WebRoot目录下编写fail.jsp文件 失败后跳转

    <body>
       Login Failed! <br>
      </body>

    7、修改web.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
     xmlns="
    http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="
    http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee 
     
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
     
      <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>CheckAccount</servlet-name>
           <servlet-class>com.ht.servlet.CheckAccount</servlet-class>
         </servlet>

         <servlet-mapping>
           <servlet-name>CheckAccount</servlet-name>
           <url-pattern>/login</url-pattern>
         </servlet-mapping>

     
    </web-app>

    参考:http://blog.sina.com.cn/s/articlelist_1549519111_8_1.html

  • 相关阅读:
    SpringMVC初写(二)映射类型、限制和数据绑定
    SpringMVC初写(一)SpringMVC的配置方式
    Spring框架初写
    CSS定位属性
    vuecli项目用yarn运行报错原因
    JS中出现NaN问题怎么解决?
    vue语法 `${ }` (模版字符串)
    Element-UI中Select选择器详解
    vue element-ui Radio单选框组件默认值选不中的原因:混用字符串和数字
    word-wrap属性允许长的内容可以自动换行
  • 原文地址:https://www.cnblogs.com/tantanba/p/6187918.html
Copyright © 2011-2022 走看看