关闭

JavaWeb开发基础:Servlet登录小实例

标签: servletjava webjavaservlet登录实例
243人阅读 评论(0) 收藏 举报
分类:

Servlet主要步骤

创建Servlet类

package com.login.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet{
	@Override
	public void init() throws ServletException {
		super.init();
	}
	
	@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 {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String forward = null;
		if(username.equals("haha") && password.equals("1234")){
			forward = "/jsp/success.jsp";
		}else{
			forward = "/jsp/error.jsp";
		}
		//请求转发
		RequestDispatcher rd = req.getRequestDispatcher(forward);
		rd.forward(req, resp);
	}

	@Override
	public void destroy() {
		super.destroy();
	}
	
}

Servlet类要继承HttpServlet,重写doGet和doPost方法

String username = req.getParameter("username");
String password = req.getParameter("password");

上面的代码用于获取表单页面传过来的参数

forward = "/jsp/success.jsp";
RequestDispatcher rd= req.getRequestDispatcher(forward);
rd.forward(req, resp);

上面的代码用于请求转发,跳转到success页面进行处理,实现跳转的方法还有重定向

resp.sendRedirect(req.getContextPath()+forward);

请求转发和重定向的区别:

请求转发只请求一次,所以success页面能获得username的值,重定向请求两次,所以success页面username的值为null;请求转发只能在同一应用中跳转,重定向能跳转到不同应用中的页面


在web.xml中配置Servlet

<servlet>
       <servlet-name>LoginServlet</servlet-name>
       <servlet-class>com.login.servlet.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
       <servlet-name>LoginServlet</servlet-name>
       <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

两个<servlet-name>中的值要一样,<servlet-class>是servlet类完整的包路径,<url-pattern>为访问servlet的入口


创建jsp页面

login.jsp

<form action="<%=request.getContextPath()%>/LoginServlet" method="post">
      用户名:<input name="username" type="text"/><br/>
      密 码:<input name="password" type="text"/><br/>
      <input type="submit" value="提交"/>
      <input type="reset" value="重置"/>
</form>

action="<%=request.getContextPath()%>/LoginServlet"与<url-pattern>的路径一致,就可以调用相应的servlet

 

加上JDBC,完成业务逻辑

JDBC教程请参考http://blog.csdn.net/wenyijiao/article/details/49892445

编写业务逻辑处理类

package com.login.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.login.dao.UserDao;
import com.login.dao.impl.UserDaoImpl;
import com.login.entity.UserEntity;
import com.login.util.ConnectionFactory;

public class CheckUserService {
	public boolean Check(UserEntity user){
		Connection conn = null;
		ResultSet rs = null;
		try {
			conn = ConnectionFactory.getInstance().getConnection();
			conn.setAutoCommit(false);
			UserDao userDao = new UserDaoImpl();
			rs = userDao.select(conn, user);
			conn.commit();
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				rs.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	return false;
	}
}

Check方法传入用户名和密码参数,调用Dao对象查询数据库中是否存在满足要求的记录,返回相应的bool值


编写CheckLoginServlet类

package com.login.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.login.entity.UserEntity;
import com.login.service.CheckUserService;

public class CheckLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CheckUserService cku = new CheckUserService();
       
    public CheckLoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		UserEntity user = new UserEntity();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String forward = null;
		if(username.equals("")|| password.equals("")){
			//请求转发
			forward = "/jsp/error.jsp";
			req.setAttribute("msg", "用户名或密码为空!");
		}else{
			user.setName(username);
			user.setPassword(password);
			boolean check = cku.Check(user);
			if(check){
				//请求转发
				forward = "/jsp/success.jsp";
			}else{
				//请求转发
				forward = "/jsp/error.jsp";
				req.setAttribute("msg", "用户名或密码错误!");
				
				System.out.println("====="+username+"---"+password);
			}
		}
		RequestDispatcher rd = req.getRequestDispatcher(forward);
		rd.forward(req, resp);
	}
}

CheckLoginServlet的doPost方法调用CheckUserService的check方法,通过返回的值判断用户名和密码是否正确,进行相应的跳转

在web.xml中配置相应的servlet,修改login.jsp页面完成登录程序

完整项目源码请参考http://pan.baidu.com/s/1bnnz4Ub

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6710次
    • 积分:185
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论
    文章存档