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

📄 loginclservlet.java

📁 这事本人自己开发的用户管理方面的Java程序
💻 JAVA
字号:
//这是控制器,主要完成对用户身份的验证
//控制器本身是不会去完成业务逻辑,它主要是去调用 模型,完成对数据的处理
package com.sp.controller;

import com.sp.model.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

public class LoginClServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @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 doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//得到用户名和密码
		String u=request.getParameter("username");
		System.out.println("username: "+u+";");
		u=Tools.getNewString(u);
		 
		String p=request.getParameter("passwd");
		System.out.println("password: "+p+";");
		System.out.println("u="+u+"p="+p);
		//使用模型(UserBeanCl),完成对用户的验证
		//1.创建一个UserBeanCl对象
		UserBeanCl ubc=new UserBeanCl();
		//调用方法
		if(ubc.checkUser(u, p)){
			
			System.out.println("这是是使用的控制器完成验证");
			
			//在跳转到wel.jsp页面时,就把要显示的数据,给wel.jsp
			//准备好
			ArrayList al=ubc.getUsersByPage(1);
			int pageCount=ubc.getPageCount();
			//将al,pageCount,pageNow放入request中
			request.setAttribute("result", al);
			request.setAttribute("pageCount", pageCount+"");
			request.setAttribute("pageNow", "1");
			//合法
			//转向.
			//response.sendRedirect("wel.jsp");
			
			//将用户名放入session,以备后用
			request.getSession().setAttribute("myName", u);
			
			//因为sendRedirect方法效率不高,所以软件公司常常是转发方法
			//这种方法,它的效率高,同时request中的对象还可以在下一页面使用
			request.getRequestDispatcher("main.jsp").forward(request, response);
			
		}else{
			
			//不合法
			//response.sendRedirect("login.jsp");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		
	}

	/**
	 * 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 {

		//和二为一
		this.doGet(request, response);
	}

}

⌨️ 快捷键说明

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