2020-03-29

登录、字符集过滤器

package com.good.domain;

public class User {

  private Integer id;

  private String username;

  private String password;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

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;

}

}

package com.good.controller;

import java.io.IOException;

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 com.good.domain.User;

import com.good.model.UserModel;

/**

* Servlet implementation class UserServlet

*/

@WebServlet("/UserServlet")

public class UserServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


try {

String username = request.getParameter("username");

String password = request.getParameter("password");

User user = new User();

user.setUsername(username);

user.setPassword(password);

UserModel userModel = new UserModel();

User existUser = userModel.login(user);

if(existUser == null) {

request.setAttribute("msg", "用户名或密码错误");

request.getRequestDispatcher("/login.jsp").forward(request, response);

}else {

request.getSession().setAttribute("exisetUser", existUser);

response.sendRedirect(request.getContextPath() + "/jsp/success.jsp");

}

} catch (Exception e) {

e.printStackTrace();

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}

package com.good.model;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanHandler;

import com.good.domain.User;

import com.good.utils.Utils;

/**

*

* @author 于卓霖

*

*/

public class UserModel {

public User login(User user) throws SQLException {

QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());

String sql = "select * from user where username=? and password=?";

User existUser = queryRunner.query(sql, new BeanHandler(User.class),user.getUsername(),user.getPassword());

return existUser;

}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

Insert title here

登录页面

${ msg }

   

 

   

     

   

   

       

       

       

       

       

 

用户名
密码

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

Insert title here

您已经登录程璐!欢迎:${ existUser.username }

提交数据

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

Insert title here

  姓名:

 

  姓名:

 

package com.good.controller;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class ServletDemo1

*/

@WebServlet("/ServletDemo1")

public class ServletDemo1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  String name = request.getParameter("name");

  System.out.println("GET方式接收的名称:" + name);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String name = request.getParameter("name");

  System.out.println("POST方式接收的名称:" + name);

}

}

package com.good.filter;

import java.io.UnsupportedEncodingException;

import javax.servlet.AsyncContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequestWrapper;

public class MyHttpServletRequest extends HttpServletRequestWrapper{

    private HttpServletRequest request;

public MyHttpServletRequest(HttpServletRequest request) {

super(request);

this.request = request;

}

  @Override

public String getParameter(String name) {

      String method = request.getMethod();

      if("GET".equals(method)) {

      String value = super.getParameter(name);

      try {

//      value = new String(value.getBytes("ISO-8859-1"),"UTF-8");

      value = new String(value.getBytes("UTF-8"),"UTF-8");

      }catch(UnsupportedEncodingException e) {

      e.printStackTrace();

      }

      return value;

      }else if("POST".equals(method)) {

      try {

      request.setCharacterEncoding("UTF-8");

      }catch(UnsupportedEncodingException e) {

      e.printStackTrace();

      }

      }

  return super.getParameter(name);

}

}

package com.good.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

public class GenericEncodingFilter implements Filter{

@Override

public void destroy() {

// TODO Auto-generated method stub

}

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

    MyHttpServletRequest myReq = new MyHttpServletRequest(req);

    chain.doFilter(myReq, response);

}

@Override

public void init(FilterConfig filterConfig) throws ServletException {

// TODO Auto-generated method stub

}

}

package com.good.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import com.good.domain.User;

public class MyPrivilegeFilter implements Filter{

@Override

public void destroy() {

// TODO Auto-generated method stub

}

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain Chain)

throws IOException, ServletException {

  HttpServletRequest req = (HttpServletRequest) request;

  User existUser = (User) req.getSession().getAttribute("existUser");

  if(existUser == null) {

  req.setAttribute("msg", "您还没有登录!没有权限访问!");

  req.getRequestDispatcher("/login.jsp").forward(request, response);

  }else {

  Chain.doFilter(request,response);

  }

}

@Override

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}

你可能感兴趣的:(2020-03-29)