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>(User.class),user.getUsername(),user.getPassword());

return existUser;

}

}

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

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>登录页面</h1>

<h3><font color="red">${ msg }</font></h3>

<form action="${ pageContext.request.contextPath }/UserServlet" method="post">

    <table border="1" width="400">

  <tr>

    <td>用户名</td>

      <td><input type="text" name="username"></td>

    </tr>

    <tr>

        <td>密码</td>

        <td><input type="password" name="password"></td>

        </tr>

        <tr>

        <td colspan="2"><input type="submit" value="登录"></td>

</tr>

  </table>

</form>

</body>

</html>

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

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

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

<a href="${ pageContext.request.contextPath }/jsp/sub.jsp">提交数据</a>

</body>

</html>

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

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1></h1>

<form action="${ pageContext.request.contextPath }/ServletDemo1" method="get">

  姓名:<input type="text" name="name"><br>

  <input type="submit" value="提交">

</form>

<h1></h1>

<form action="${ pageContext.request.contextPath }/ServletDemo1" method="get">

  姓名:<input type="text" name="name"><br>

  <input type="submit" value="提交">

</form>

</body>

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

}

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容