zoukankan      html  css  js  c++  java
  • 第七周jsp作业

    registerServlet

    package com.kjschool.javaweb.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @WebServlet(name = "registerServlet")
    public class registerServlet extends HttpServlet {
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            String realname= new String(request.getParameter("realname").getBytes("iso8859-1"),"UTF-8");
            String name= new String(request.getParameter("name").getBytes("iso8859-1"),"UTF-8");
            String brith=request.getParameter("brith");
    
            int count=t_userDao.insertuser(username,password,realname);
            t_userDao.insert(name,brith);
            if (count==1){
                HttpSession session=request.getSession();
                session.setAttribute("realname",realname);
                response.sendRedirect(request.getContextPath() + "/firstpage.jsp");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    }

    t_userDao.insertuser()

    public static int insertuser(String username, String password,String realname) {
            Connection conn = null;
            PreparedStatement ps = null;
            int count = 0;
            try {
                //获取连接
                conn = DBUtils.getConnection();
                //获取预编译的数据库操作对象
                String sql = "insert into t_users (login_name,login_pwd,real_name) values (?,?,?)";
    
                ps = conn.prepareStatement(sql);
                //传值
                ps.setString(1, username);
                ps.setString(2, password);
                ps.setString(3,realname);
                count = ps.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                DBUtils.close(conn, ps, null);
            }
            return count;
        }

    DBUtils

    package com.kjschool.javaweb.servlet;
    
    import java.sql.*;
    import java.util.ResourceBundle;
    
    public class DBUtils {
        private DBUtils(){
    
        }
        //类加载时绑定资源属性文件
        private static ResourceBundle bundle=ResourceBundle.getBundle("resources.db");
        //注册驱动
        static{
            try {
                Class.forName(bundle.getString("driver"));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        //获取数据库连接对象
        public static Connection getConnection() throws SQLException {
            String url=bundle.getString("url");
            String user=bundle.getString("user");
            String password=bundle.getString("password");
            Connection conn= DriverManager.getConnection(url,user,password);
            return conn;
        }
        //释放资源
        public  static void close(Connection conn, Statement stmt, ResultSet rs){
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    
    }

    register.jsp

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                span{
                    font-size: 15px;
                    color: #ff0000;
                }
            </style>
        </head>
        <body>
            <script type="text/javascript">
                window.onload=function(){
                    var namespan=document.getElementById("namespan");
                    var usernameElt=document.getElementById("username");
                    usernameElt.onblur=function(){
                        var username=usernameElt.value;
                        username=username.trim();
                        if (username=="") {
                            namespan.innerHTML="用户名不能为空";
                        } else{
                            if (username.length<6||username.length>12) {
                                namespan.innerHTML="用户名在[6,12]之间"
                            }else{
                                var regExp=/^[a-zA-Z0-9]+$/
                                var ok=regExp.test(username)
                                if (!ok) {
                                    namespan.innerHTML="用户名只能由字母和数字组成"
                                }
                            }
                        }
                    }
                    usernameElt.onfocus=function(){
                        namespan.innerHTML=""
                    }
                    
                    
                    var passspan=document.getElementById("passspan");
                    var passwordelt=document.getElementById("secondpassword");
                    passwordelt.onblur=function(){    
                        var password=document.getElementById("password").value;
                        var secondpassword=passwordelt.value;
                        password=password.trim();
                        secondpassword=secondpassword.trim();
                        /*if (secondpassword=="") {
                            passspan.innerHTML="确认密码不能为空";
                        }else{*/
                            if (password==""||secondpassword=="") {
                            passspan.innerHTML="密码不能为空";
                            }else{
                                if (password!=secondpassword) {
                                    passspan.innerHTML="两次密码不一致";
                                } 
                            }
                        /*}*/        
                    }
                    passwordelt.onfocus=function(){
                        passspan.innerHTML="";
                    }
                    
                    document.getElementById("zhuce").onclick=function(){
                        usernameElt.focus();
                        usernameElt.blur();
                        document.getElementById("secondpassword").focus();
                        document.getElementById("secondpassword").blur();
                        if (namespan.innerHTML==""&& passspan.innerHTML=="") {
                            var formobj=document.getElementById("userform")
                            formobj.submit();
                        }
                    
                }
            }
            </script>
            <form action="register"  id="userform" method="post">
            用户名:<input type="text" name="username" id="username"  /><span id="namespan">    </span><br />
            密码: <input type="password" name="password" id="password" /></span><br />
            确认密码: <input type="password"  id="secondpassword"  /><span id="passspan"></span><br />
            昵称:<input type="text" id="realname" name="realname"><br>
                姓名:<input type="text" name="name"><br>
                生日:<input type="text" name="brith"><br>
            <input type="button" name="zhuce" id="zhuce" value="注册" />
                <input type="reset" value="重置">
            </form>
        </body>
    </html>

    OneFilter

    ackage com.kjschool.javaweb.servlet;
    
    import com.sun.deploy.net.HttpRequest;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    import java.util.logging.LogRecord;
    
    public class OneFilter implements Filter {
    
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request= (HttpServletRequest) servletRequest;
            HttpSession session=null;
            //获取uri
            String uri=request.getRequestURI();
            if ("login".indexOf(uri)!=-1||"/examination_war_exploded/".equals(uri)) {
                filterChain.doFilter(servletRequest,servletResponse);
                return;
            }
            session = request.getSession(false);
            if(session!=null){
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                request.getRequestDispatcher("/loginerror.jsp").forward(servletRequest,servletResponse);
            }
    
        }
    
        @Override
        public void destroy() {
    
        }
    
    }

  • 相关阅读:
    Metricbeat
    Flask安装与基本配置
    web框架
    git
    占位
    算法
    面试
    CMDB
    order by关键字排序优化
    动态主机配置协议-DHCP
  • 原文地址:https://www.cnblogs.com/lqh123456/p/14676339.html
Copyright © 2011-2022 走看看