⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginservlet.java

📁 servlet实现网上书店查询
💻 JAVA
字号:
package com.ghy.bookstore.user.web;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;

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 com.ghy.bookstore.service.BookStoreService;
import com.ghy.bookstore.service.BookStoreServiceFactory;
import com.ghy.bookstore.user.model.UserVO;

public class LoginServlet extends HttpServlet {

	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public String invalid(UserVO user) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException 
	{
          BookStoreService bsService=BookStoreServiceFactory.getBookStoreService();
          String message="";
          if(user.getName()==null||user.getName().trim().equals(""))
        	  message="用户名为空";
          if(!message.equals(""))
        	  return message;
          if(user.getPassword()==null||user.getPassword().trim().equals(""))
        	  message="密码为空";
          if(!message.equals(""))
        	  return message;
          if(!bsService.login(user))
        	  message="用户名或密码错误";
          if(!message.equals(""))
        	  return message;
          else 
        	  return "";
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
			{
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		UserVO user=new UserVO();
		user.setName(request.getParameter("username"));
		user.setPassword(request.getParameter("userpassword"));
		String message;
		try {
			message = this.invalid(user);
		
		if(!message.equals(""))
		{
			request.getRequestDispatcher("/jsp/user/login.jsp?message="+URLEncoder.encode(message, "utf-8")).forward(request, response);
			return ;
		}
		HttpSession session=request.getSession();
		BookStoreService bsService=BookStoreServiceFactory.getBookStoreService();
		session.setAttribute("CurrentUser", bsService.findUserByName(user.getName()));
		response.sendRedirect(request.getContextPath()+"/book/bookmain");
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -