• jsp第十次作业


     

    package com.dhy.servlet;
    
    import java.io.IOException;
    import java.sql.Connection;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    
    import com.dhy.dao.BaseDao;
    import com.dhy.dao.UsersDao;
    import com.dhy.bean.Users;
    
    public class Loginservlet extends HttpServlet {
        //声明
        private final String SUCCESS_VIEW = "MyJsp.jsp";
        private final String ERROR_VIEW = "index.jsp";
        private  UsersDao userDao = new UsersDao();
        private BaseDao basedao = new BaseDao();
       
     
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           this.doPost(request, response);
       }
    
       
    public void doPost(HttpServletRequest request,HttpServletResponse response)
           throws ServletException,IOException{
               //1.防止乱码
           request.setCharacterEncoding("utf-8");
           response.setContentType("text/html;charset=utf-8");
           //2.获取页面信息
            String username = request.getParameter("username");
           String password = request.getParameter("password");
           //3.获取登录用户信息
           request . getSession().setAttribute ( "UserName" , username ) ; 
           request.getSession().setAttribute("password", password);
           Connection con=null;
              //4.验证操作,将用户信息封装到currentUser
              try {
                  Users user = new Users(username,password);
                   con = basedao.getConnection();
                   Users currentUser = userDao.login(con, user);
                   if (null == currentUser) {
                       //5.输入信息错误
                       request.setAttribute("error", "用户名或密码错误");
                       request.setAttribute("userName", username);
                       request.setAttribute("password", password);
                       RequestDispatcher dispatcher = request.getRequestDispatcher(ERROR_VIEW);
                       dispatcher.forward(request, response);
                   }else {
                       //6.输入正确的信息
                       HttpSession session = request.getSession();
                       session.setAttribute("currentUser",currentUser);
                       response.sendRedirect(SUCCESS_VIEW );
                   }
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
        
    }
    
    }
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
     
       <body>
        <h1>登录</h1>
        <form action="textServlet" method="post">
            用户名:<input type="text" name="username" value="${username}" /><br/>
            密   码:<input type="password" name="password" value=""/><br/>
        <input type="submit" value="登录"/>
        <a href="regist.jsp">注册</a>
        </form>
        <font color="red">${error}</font>
      </body>
    
    
    
    </html>
    package com.dhy.bean;
    
    public  class Users {
        String username;
        String password;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public Users(String username,String password) {
            this.username=username;
            this.password=password;
            // TODO Auto-generated constructor stub
        }
        public Users() {
            
            // TODO Auto-generated constructor stub
        }
    
    }
  • 相关阅读:
    Java 第十一届 蓝桥杯 省模拟赛 梅花桩
    Java 第十一届 蓝桥杯 省模拟赛 梅花桩
    Java 第十一届 蓝桥杯 省模拟赛 梅花桩
    Java 第十一届 蓝桥杯 省模拟赛 元音字母辅音字母的数量
    Java 第十一届 蓝桥杯 省模拟赛 元音字母辅音字母的数量
    Java 第十一届 蓝桥杯 省模拟赛 元音字母辅音字母的数量
    Java 第十一届 蓝桥杯 省模拟赛 最大的元素距离
    Java 第十一届 蓝桥杯 省模拟赛 递增序列
    Java 第十一届 蓝桥杯 省模拟赛 递增序列
    Java 第十一届 蓝桥杯 省模拟赛 最大的元素距离
  • 原文地址:https://www.cnblogs.com/DOCEAN/p/12984004.html
Copyright © 2020-2023  润新知