web实现个人信息的查看

查看个人信息

内含JDBC的封装,详细资料请看JDBC工具类的封装

1、accountId.jsp(从request域中获取用户的个人信息)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="accountManager.jsp"%>


    Title


    
  • 姓名: ${requestScope.user.username}  修改


  • 密码: ${requestScope.user.password}  


  • 性别:


  • 邮箱: ${requestScope.user.email}  


2、LoginServlet(登录逻辑处理,把从数据库中返回的用户Id放到session中)

package com.university.controller;

import com.university.dao.UserDao;
import com.university.entity.Users;

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

public class LoginServlet extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        UserDao dao = new UserDao();
        int userId = 0;
        //1、调用请求对象对请求体使用utf-8字符集进行重新编辑
        request.setCharacterEncoding("UTF-8");
        //2、调用请求对象读取请求体参数信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //3、调用Dao将查询验证信息推送到数据库服务器上
        userId = dao.login(username,password);
        //4、调用响应对象,根据验证结果将不同资源文件地址写入到响应头,交给浏览器
        response.setContentType("text/html;charset=UTF-8");
        if (userId != 0){
     
            //在判定来访用户身份合法后,通过请求对象向Tomcat申请为当前用户申请一个HttpSession
            request.getSession().setAttribute("userId",userId);
            response.sendRedirect(request.getContextPath() + "/menus.html");
        } else {
     
            response.getWriter().println("用户名或密码错误");
        }
    }
}

3、UserDao(数据库处理,登录返回的是用户Id)

package com.university.dao;

import com.university.entity.Users;
import com.university.util.JdbcUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
     
    private JdbcUtil util = new JdbcUtil();
    //用户登录
    public int login(String username,String password){
     
        String sql = "select * from users where username=? and password=?";
        int userId = 0;
        ResultSet rs = null;
        try {
     
            PreparedStatement ps = util.createStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while (rs.next()){
     
                userId = rs.getInt("userId");
                rs.getString("username");
                rs.getString("password");
            }
        } catch (SQLException e) {
     
            e.printStackTrace();
        } finally {
     
            util.close(rs);
        }
        return userId;
    }

    //查看个人信息
    public Users findID(String userId){
     
        String sql = "select * from users where userId=?";
        ResultSet rs = null;
        Users user = null;
        try {
     
            PreparedStatement ps = util.createStatement(sql);
            ps.setInt(1,Integer.valueOf(userId));
            rs = ps.executeQuery();
            while (rs.next()){
     
                Integer userId = rs.getInt("userId");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String sex = rs.getString("sex");
                String email = rs.getString("email");
                user = new Users(userId,username,password,sex,email);
            }
        } catch (SQLException e) {
     
            e.printStackTrace();
        } finally {
     
            util.close(rs);
        }
        return user;
    }
}

4、UserFindIDServlet(从session中获取用户Id,再把id传给数据库作查询处理,返回的值是User对象,然后再把User对象放到request域中)

package com.university.controller;

import com.university.dao.UserDao;
import com.university.entity.Users;

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 java.io.IOException;

public class UserFindIDServlet extends HttpServlet {
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        UserDao dao = new UserDao();
        Users users = null;
        HttpSession session = request.getSession(false);
        String userId = String.valueOf(session.getAttribute("userId"));
        users = dao.findID(userId);
        request.setAttribute("user",users);
        request.getRequestDispatcher("/accountId.jsp").forward(request,response);
    }
}

5、Servlet配置

<servlet>
    <servlet-name>LoginServletservlet-name>
    <servlet-class>com.university.controller.LoginServletservlet-class>
servlet>
<servlet>
    <servlet-name>UserFindIDServletservlet-name>
    <servlet-class>com.university.controller.UserFindIDServletservlet-class>
servlet>
<servlet-mapping>
    <servlet-name>LoginServletservlet-name>
    <url-pattern>/loginurl-pattern>
servlet-mapping>
<servlet-mapping>
    <servlet-name>UserFindIDServletservlet-name>
    <url-pattern>/user/IDurl-pattern>
servlet-mapping>

6、测试

(1)登录

web实现个人信息的查看_第1张图片

(2)查看个人信息

web实现个人信息的查看_第2张图片

你可能感兴趣的:(业务实现,servlet,jsp,数据库)