欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDBC操作之servlet页面

程序员文章站 2024-03-20 12:55:34
...

package com.hp.servlet;

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 javax.servlet.http.HttpSession;
import javax.websocket.Session;

import com.hp.bean.Nanyang;
import com.hp.dao.NanyangDao;

@WebServlet("/best")
public class DengLu extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//中文格式代码
req.setCharacterEncoding(“utf-8”);
resp.setCharacterEncoding(“utf-8”);

	String userName = req.getParameter("userName");
	String pass = req.getParameter("pass");
	
	//JDBC操作
	NanyangDao nanyangDao=new NanyangDao();
	Nanyang nanyang=new Nanyang();
	
	nanyang.setUsername(userName);
	nanyang.setPass(pass);
	
	Nanyang denglu =nanyangDao.denglu(nanyang);
	System.out.println(denglu);
	
	HttpSession session = req.getSession();//把请求放入session
	session.setAttribute("msg",userName );
	
	if(denglu.getUsername()!=null) {
		System.out.println("登陆成功!");
		//跳转页面
		resp.sendRedirect("Chenggong.jsp");
	}else {
		System.out.println("登陆失败!");
		resp.sendRedirect("Shibai.jsp");
	}
	
	
}

}