servlet学习(二)

21 篇文章 0 订阅
5 篇文章 0 订阅

servlet学习(二)

标签: servlet


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

目录结构

目录结构

登录

登录界面 login.jsp

<%--
  User: Fate
  Date: 2016/7/5
  Time: 10:30
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login</title>
    <meta charset="utf-8">
    <meta name="keyword" content="登录">
    <meta name="description" content="">
    <!--css/js-->
    <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>
<!--S form-->
<div class="my_text">
    <a>登录界面</a>
</div>
<!--E form-->
<!--S form-->
<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>
<!--E form-->
<!--S span-->
<div class="span">
    <span>don't have account?</span>
    <a href="register.jsp">Now register!</a>
</div>
<!--E span-->
</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>login</title>
    <meta charset="utf-8">
    <meta name="keyword" content="注册">
    <meta name="description" content="">
    <!--css/js-->
    <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>
<!--S text-->
<div class="my_text">
    <a>注册界面</a>
</div>
<!--E text-->
<!--S form-->
<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>
<!--E form-->
<!--S span-->
<div class="span">
    <span>already have an account?</span>
    <a href="login.jsp">Go to login!</a>
</div>
<!--E span-->
</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

<?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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>LoginCl</servlet-name>
        <servlet-class>controller.LoginControllerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginCl</servlet-name>
        <url-pattern>/loginControllerServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>registerCL</servlet-name>
        <servlet-class>controller.RegisterControllerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>registerCL</servlet-name>
        <url-pattern>/registerControllerServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

页面示例

页面

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值