【jsp】データベースを呼び出してユーザログイン機能を実現する(ログインインタフェースはテンプレートを使用する)


login.jsp







    rendshop    





	$(function(){
    $('.loginbox').css({'position':'absolute','left':($(window).width()-692)/2});
	$(window).resize(function(){  
    $('.loginbox').css({'position':'absolute','left':($(window).width()-692)/2});
    })  
});  
 







    

loginservlet.java

package com.zzxtit.shop.sys.controller;

import java.io.IOException;
import java.sql.SQLException;

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 org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.zzxtit.common.user.UserInfor;
import com.zzxtit.common.util.DBUtil;

@WebServlet("/dologin.action")
public class LoginServlet extends HttpServlet{

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

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		System.out.println(userName);
		String passwd = request.getParameter("passwd");
		System.out.println(passwd);
		QueryRunner qr = new QueryRunner(DBUtil.getDS());
		String sql = "select * from user_infor where user_name = ? and password = ?";
		try {
			if(qr.query(sql, new BeanHandler(UserInfor.class),userName,passwd) != null) {
				
				response.sendRedirect("success.jsp");
			}else {
				
				response.sendRedirect("Failure.jsp");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

characterencodingfilter.java
package com.zzxtit.common.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;

public class CharacterEncodingFilter implements Filter{
	
	private static String encoding = "utf-8";
	
	@Override
	public void destroy() {
		
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
		System.out.println("asdfasgdawer");
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		if(config.getInitParameter("encoding") != null) {
			encoding = config.getInitParameter("encoding");
		}
		
	}

}

web.xml


	
	
	
		5
	
	
	
	
		index.jsp
		login.jsp