servlet学习(二)

servlet学习(二)

标签: servlet


servlet学习(二)_第1张图片
之前使用servlet写了一个登录注册简单程序,现在把他它升下级,使用jsp+servlet来完成,没用js验证,采用的是后台servlet验证,因为请求传递中文参数会乱码,又不想多进行以此编码,所以这里我传递了一个状态码,用小脚本直接页面赋值。

目录结构

servlet学习(二)_第2张图片

登录

登录界面 login.jsp

<%--
  User: Fate
  Date: 2016/7/5
  Time: 10:30
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>logintitle>
    <meta charset="utf-8">
    <meta name="keyword" content="登录">
    <meta name="description" content="">
    
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background: #a7a7a7 repeat;
        }

        /*S text*/
        .my_text {
            margin: 30px auto;
            text-align: center;
            font-size: 20px;
            font-family: "Microsoft YaHei", sans-serif;
            color: white;
        }

        /*E text*/
        /*S form*/
        .my_form {
            width: 600px;
            height: 100px;
            margin: 30px auto;
            text-align: center;
        }

        .return_msg {
            color: red;
            text-align: center;
            font-family: "Microsoft YaHei", sans-serif;
            font-size: 18px;
            font-size: 16px;
            background: #a7a7a7;
            border: none;
            outline: none;
        }

        .my_input {
            width: 500px;
            height: 30px;
            border-radius: 3px 3px 3px 3px;
            border: none;
            outline: none;
            margin: 6px;
        }

        .my_button {
            border-radius: 3px 3px 3px 3px;
            width: 110px;
            height: 36px;
            background: #3cbd38;
            border: none;
            outline: none;
            cursor: pointer;
            text-align: center;
            color: white;
        }

        .my_button:hover {
            background: #39ad35;
        }

        /*E form*/
        /*S span*/
        .span {
            margin-top: 50px;
            text-align: center;
        }

        /*E span*/
    style>
head>
<body>

<div class="my_text">
    <a>登录界面a>
div>


<form action="loginControllerServlet" method="post" class="my_form">
    <%
        String msg = "";
        String code = request.getParameter("code");
        if ("10002".equals(code)) {
            msg = "用户名或密码不正确";
        }
    %>
    <input class="return_msg" value="<%=msg%>" placeholder=" ">
    <input name="username" placeholder="please enter your name" type="text" class="my_input">
    <input name="password" placeholder="please enter password" type="password" class="my_input">
    <input type="submit" value="submit" class="my_button">
    <input type="reset" value="reset" class="my_button">
form>


<div class="span">
    <span>don't have account?span>
    <a href="register.jsp">Now register!a>
div>

body>
html>

登录控制 LoginControllerServlet

package controller;

import service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Fate on 2016/7/1.
 */
public class LoginControllerServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        LoginService LoginService = new LoginService();
        boolean result = LoginService.login(username, password);
        if (result) {
            resp.sendRedirect("wel.jsp");
            return;
        }
        String code = "10002";
        resp.sendRedirect("login.jsp?code=" + code);
    }

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

登录持久层(做了一些小优化) LoginService

package service;

import util.ConnectionUtil;

import java.sql.*;

/**
 * Created by Fate on 2016/7/4.
 */
public class LoginService {
    public boolean login(String name, String password) {
        String sql = "select * FROM user where name=? and password=?";
        Connection connection = ConnectionUtil.getConnetion();
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, password);
            resultSet = pstmt.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}

注册

register.jsp

<%--
  User: Fate
  Date: 2016/7/5
  Time: 13:44
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>logintitle>
    <meta charset="utf-8">
    <meta name="keyword" content="注册">
    <meta name="description" content="">
    
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background: #a7a7a7 repeat;
        }

        /*S text*/
        .my_text {
            margin: 30px auto;
            text-align: center;
            font-size: 20px;
            font-family: "Microsoft YaHei", sans-serif;
            color: white;
        }

        /*E text*/
        /*S form*/
        .my_form {
            width: 600px;
            height: 140px;
            margin: 30px auto;
            text-align: center;
        }

        .return_msg {
            color: red;
            text-align: center;
            font-family: "Microsoft YaHei", sans-serif;
            font-size: 16px;
            background: #a7a7a7;
            border: none;
            outline: none;
        }

        .my_input {
            width: 500px;
            height: 30px;
            border-radius: 3px 3px 3px 3px;
            border: none;
            outline: none;
            margin: 6px;
        }

        .my_button {
            border-radius: 3px 3px 3px 3px;
            width: 110px;
            height: 36px;
            background: #3cbd38;
            border: none;
            outline: none;
            cursor: pointer;
            text-align: center;
            color: white;
        }

        .my_button:hover {
            background: #39ad35;
        }

        /*E form*/
        /*S span*/
        .span {
            margin-top: 50px;
            text-align: center;
        }

        /*E span*/
    style>
head>
<body>

<div class="my_text">
    <a>注册界面a>
div>


<div>
    <form action="registerControllerServlet" method="post" class="my_form">
        <%
            String code = request.getParameter("code");
            String msg = "";
            if ("10001".equals(code)) {
                msg = "用户已存在";
            }
            if ("10000".equals(code)) {
                msg = "必要参数为空";
            }
        %>
        <input class="return_msg" value="<%=msg%>" placeholder=" ">
        <input name="username" placeholder="please enter your name" type="text" class="my_input">
        <input name="password" placeholder="please enter password" type="password" class="my_input">
        <input name="age" placeholder="please enter your age" type="password" class="my_input">
        <input type="submit" value="submit" class="my_button">
        <input type="reset" value="reset" class="my_button">
    form>
div>


<div class="span">
    <span>already have an account?span>
    <a href="login.jsp">Go to login!a>
div>

body>
html>

RegisterControllerServlet

package controller;

import service.RegisterService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Fate on 2016/7/4.
 */
public class RegisterControllerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String ages = req.getParameter("age");
        String code="";
        int age = 0;
        if ("".equals(username) || "".equals(password)) {
            code="10000";
            resp.sendRedirect("register.jsp?code="+code);
            return;
        }
        if (!"".equals(ages)) {
            age = Integer.parseInt(ages);
        }
        RegisterService register = new RegisterService();
        boolean result = register.register(username, password, age);
        if (result) {
            resp.sendRedirect("wel.jsp");
            return;
        }
        code = "10001";
        resp.sendRedirect("register.jsp?code="+code);
    }

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

RegisterService

package service;

import util.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Fate on 2016/7/4.
 */
public class RegisterService {
    public boolean register(String username, String password, int age) {
        String sql = "INSERT INTO user (name,password,age) VALUES (?,?,?)";
        String sqls = "select *from user where name=?";
        Connection connection = ConnectionUtil.getConnetion();
        ResultSet resultSet = null;
        PreparedStatement pstmts = null;
        PreparedStatement pstmt = null;
        try {
            pstmts = connection.prepareStatement(sqls);
            pstmts.setString(1, username);
            resultSet = pstmts.executeQuery();
            if (resultSet.next()) {
                System.out.println("用户已存在");
                return false;
            }
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setInt(3, age);
            int result = pstmt.executeUpdate();
            return result == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (pstmts != null) {
                    pstmts.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

配置

web.xml


<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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>LoginClservlet-name>
        <servlet-class>controller.LoginControllerServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>LoginClservlet-name>
        <url-pattern>/loginControllerServleturl-pattern>
    servlet-mapping>

    <servlet>
        <servlet-name>registerCLservlet-name>
        <servlet-class>controller.RegisterControllerServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>registerCLservlet-name>
        <url-pattern>/registerControllerServleturl-pattern>
    servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.jspwelcome-file>
    welcome-file-list>
web-app>

页面示例

servlet学习(二)_第3张图片

你可能感兴趣的:(学习,java,servlet)