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

📄 signonservlet.java

📁 一个简易的网上书店(jsp+sqlservler+struts)。
💻 JAVA
字号:
package com.webshop.servlet;

import java.io.IOException;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;

import com.webshop.db.AccountDAO;
import com.webshop.domain.Account;
import com.webshop.forms.*;
 
/**
 * @version 	1.0
 * @author
 */
public class SignonServlet extends HttpServlet {

     String defaultPage="/index.jsp";
	 AccountDAO accountBean=new AccountDAO(); 
	/**
	* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
			doPost(req,resp);

	}

    public void init(ServletConfig config) throws ServletException {
    super.init(config);
     try{
     	defaultPage = config.getInitParameter("defaultPage"); 
     	 if(defaultPage==null) defaultPage="/index.jsp";
     }
     catch(Exception e)
     {
     	defaultPage="/index.jsp";
     }
  }
	/**
	* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
			System.out.println("signon.....");
			String userId=(String)req.getParameter("userId");
			String password=(String)req.getParameter("password");
			
			String targetPage=defaultPage;
			boolean validate;
			try
			{
				validate=accountBean.signon(userId,password);
			
				if(validate)
				{
					targetPage=req.getParameter("targetPage");
					if(targetPage==null||targetPage.equals("")||targetPage.equals("null"))  targetPage=defaultPage;
					createAccountForm(userId,req,resp); 
				}
				else
				{
					targetPage="/login";
					
        	          req.setAttribute("errorMessage","请输入正确的用户名和密码!");
      
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
				validate=false;
			}	
			javax.servlet.RequestDispatcher requestDispatcher = req.getRequestDispatcher(targetPage);
	        requestDispatcher.forward(req,resp);
	        return;
	}
	private void createAccountForm(String userId,HttpServletRequest req, HttpServletResponse resp)throws Exception
	{
		Account account=accountBean.getAccountById(userId);
		AccountForm accountForm=new AccountForm();
		accountForm.setAccount(account);
		accountForm.setUserId(userId);
		accountForm.setPassword(account.getPassword());
		req.getSession().setAttribute("accountForm",accountForm);
		
	}
	

}

⌨️ 快捷键说明

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