jsp第六周作业

xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>login.htm</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.kjschool.javaweb.servlet.loginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
</web-app>

login

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>登录</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<SCRIPT language=javascript>
if(this.parent != this)//不在iframe中
{
    parent.window.location = window.location;
}
</SCRIPT>
<LINK rel=stylesheet type=text/css href="images/login.css">
<SCRIPT type=text/javascript src="images/jquery.min.js"></SCRIPT>
<LINK rel=stylesheet type=text/css href="images/login_res.css">
<META name=GENERATOR content="MSHTML 8.00.7600.16912"></HEAD>
<BODY id=login>
<DIV class=header>
<H1>威锋盘</H1></DIV>
<DIV class=login_panel>
<DIV class=login_main>
<FORM id=login_form autocomplete="false" action="login" method="post"><A class=new_user
href="http://passport.weiphone.com/?r=user/register" 
target=_blank>创建一个用户<I></I></A> 
<DIV id=login_inner class=login_inner>
<UL>
  <LI>用户名 <INPUT id=user type=text  name="username">
  </LI>
  <LI>密码 <INPUT id=pwd type=password name="password">
  </LI>
  <LI class=login_btn><INPUT value=登录 type=submit> </LI></UL></DIV></FORM>
<DIV style="DISPLAY: none" id=login_info class=login_info></DIV></DIV></DIV>
<DIV class=footer>
<DIV class=copyright>
<P>Copyright © Uimaker.com 2011</P>
<P>Develop&amp;Design WeiPhone®.com</P></DIV></DIV>

        <style type="text/css">
            .no_ie6{ background:#07070B url(/images/bg_r.png) repeat-x; color:#fff;}
            .no_ie6 ul{ padding:30px;}
            .no_ie6 li{ float:left; padding:10px 20px;}
            .no_ie6 li a{ display:block; color:#fff; text-decoration:none;}
            .no_ie6 li a:hover{ text-decoration:underline;}
        </style>
    <![endif]-->
<SCRIPT language=JavaScript src="images/c.htm"></SCRIPT>
</BODY></HTML>

login.servlet

package com.kjschool.javaweb.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "loginServlet")
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //jdbc
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        user user=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/lqh","root","root");
            String sql="select id,login_name,login_pwd from t_users where login_name=? and login_pwd=?";

            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs=ps.executeQuery();
            if (rs.next()){
               user=new user();
               user.setId(rs.getInt("id"));
               user.setLogin_name(rs.getString("login_name"));
               user.setLogin_pwd(rs.getString("login_pwd"));

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        if (user!=null){
            //获取session对象
            HttpSession session=request.getSession();
            //登录成功将用户储存到会话范围
            session.setAttribute("user",user);
            //重定向
            response.sendRedirect(request.getContextPath()+"/index.jsp");

        }else{
            response.sendRedirect(request.getContextPath()+"/loginerror.jsp");

        }
    }


}

loginerror

<html>
<head>
    <title>Title</title>
</head>
<body>
登录失败,请<a href="login.htm">重新登录</a>重新登录
</body>
</html>

loginsuccess

<html>
<head>
    <title>Title</title>
</head>
<body>
登录成功欢迎使用
</body>
</html>

user

package com.kjschool.javaweb.servlet;

public class user {
    private int id;
    private String login_name;
    private String login_pwd;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLogin_name() {
        return login_name;
    }

    public void setLogin_name(String login_name) {
        this.login_name = login_name;
    }

    public String getLogin_pwd() {
        return login_pwd;
    }

    public void setLogin_pwd(String login_pwd) {
        this.login_pwd = login_pwd;
    }
}

 

 

原文地址:https://www.cnblogs.com/lqh123456/p/14647525.html