遇见前文的注冊模块,本篇是登录模块。主要包含登录主界面,和登录相关编写的LoginAction、LoginDao和LoginService。以及配置的Filter。以下按逻辑顺序记录具体过程和代码:

一、在前文的index文件夹点击登录button后。通过javascript跳转至LoginAction。

<script type="text/javascript">
function login(){
var th = document.form1;
if(th.username.value==""){
alert("用户名不能为空! 。");
th.username.focus();
return ;
}
if(th.pswd.value==""){
alert("password不能为空! ! ");
th.pswd.focus();
return ;
}
th.action="<%=path%>/servlet/LoginAction";
th.submit();
}
</script>

二、就是关于登录这个事件,须要创建三个文件各自是LogingAction,是个Servlet。LoginService是个接口,LoginDao实现上面的接口,查询数据库。

跟前文的注冊模块一样的哈。

LogingAction.java

package com.product.login.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.product.login.dao.LoginDao;
import com.product.login.service.LoginService; public class LoginAction extends HttpServlet { private LoginService service;
/**
* Constructor of the object.
*/
public LoginAction() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* 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 { this.doPost(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 { request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String path = request.getContextPath();
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String pswd = request.getParameter("pswd");
List<Object> params = new ArrayList<Object>();
params.add(username);
params.add(pswd);
boolean flag = service.login(params);
out.println("username = " + username);
out.println("pswd = " + pswd); if(flag){
request.getSession().setAttribute("username", username);
response.sendRedirect(path + "/main.jsp");
}else{
out.println("登录失败 ");
}
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
service = new LoginDao();
} }

要点:创建一个Session,将username存进去传递给main.jsp。

也即在main.jsp的时候我要知道当前是谁在登录。

if(flag){
        request.getSession().setAttribute("username", username);
        response.sendRedirect(path + "/main.jsp");
        }

LoginService.java

package com.product.login.service;

import java.util.List;

public interface LoginService {
public boolean login(List<Object> params);
}

LoginDao.java

package com.product.login.dao;

import java.util.List;
import java.util.Map; import com.product.jdbc.dbutil.JdbcUtils;
import com.product.login.service.LoginService; public class LoginDao implements LoginService {
private JdbcUtils jdbcUtils;
public LoginDao(){
jdbcUtils = new JdbcUtils();
}
@Override
public boolean login(List<Object> params) {
// TODO Auto-generated method stub
boolean flag = false;
String sql = "select * from userinfo where username = ? and pswd = ?";
try{
jdbcUtils.getConnection();
Map<String, Object> map = jdbcUtils.findSimpleResult(sql, params);
flag = (!map.isEmpty()) ? true : false;
}catch(Exception e){
e.printStackTrace();
}finally{
jdbcUtils.releaseConn();
}
return flag;
} }

三、这样假设登录成功的话就跳转到了main.jsp界面:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuemkxMjI1NjI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

main.jsp代码例如以下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/frameset.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>后台管理系统</TITLE>
<META http-equiv=content-type content="text/html; charset=utf-8">
</HEAD>
<FRAMESET rows=105,* cols="*" bordercolor="04376E">
<FRAME name=ads marginWidth=0 marginHeight=0 src="<%=path%>/top.jsp"
frameBorder=0 noResize scrolling=no longDesc="">
<FRAMESET rows=675 cols=198,* frameborder="yes">
<FRAME name=list marginWidth=0 marginHeight=0 src="<%=path%>/left.jsp"
frameBorder=0 noResize scrolling=yes longDesc="">
<FRAME name=main marginWidth=0 marginHeight=0
src="<%=path%>/postdata.jsp" frameBorder=0 scrolling=yes longDesc="">
</FRAMESET>
<NOFRAMES>
</NOFRAMES>
</FRAMESET>
<body>
</body>
</HTML>

能够看到main.jsp在书写上利用了<frameset>标签。在里面嵌套<frame>,各自是top.jsp left.jsp postdata.jsp.使用说明參见:链接1 链接2

在index.jsp里添加了底部的jsp,使用的是iframe:

<IFRAME name="top" align="default" src="<%=path %>/bottom.jsp"
frameBorder=0 width=100% scrolling=no height=88>
<h1>&nbsp;</h1>
</IFRAME>

正好能够对照其差别,详见链接1 链接2 链接3  iframe使用时经常要和target配合使用。參见  链接1 链接2

top.jsp left.jsp postdata.jsp这三个的代码就不贴了。在源代码里。

四:filter的使用

起因是浏览器直接输入http://localhost:8080/xianfengYan/main.jsp 这时还没有登录,没有验证。我们不希望游客訪问这个界面,因此须要加个过滤器进行过滤,直接将页面跳转至index.jsp.

新建包package com.product.filter;

MyFilter.java

package com.product.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyFilter implements Filter { @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
//过滤用户的请求,推断是否登录 HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
httpServletRequest.setCharacterEncoding("utf-8");
httpServletResponse.setCharacterEncoding("utf-8");
String path = httpServletRequest.getContextPath();
String username = (String)httpServletRequest.getSession().getAttribute("username");
if(username == null){
httpServletResponse.sendRedirect(path + "/index.jsp");
}
chain.doFilter(httpServletRequest, httpServletResponse);
} @Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub } }

然后再web.xml里进行配置:

	<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.product.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/main.jsp</url-pattern>
</filter-mapping>

url-pattern里表示过滤的页面,我们指定过滤/main.jsp这个界面。

代码下载链接:http://download.csdn.net/detail/yanzi1225627/7448175

版权声明:本文博客原创文章。博客,未经同意,不得转载。

基于Servlet、JSP、JDBC、MySQL登录模块(包括使用的过滤器和配置)的更多相关文章

  1. 基于Servlet+jsp的web计算器

    基于Servlet+jsp的web计算器 这次老大为了让我们自己复习web中页面的跳转给布置得任务 天下代码一大抄,关键看你怎么抄 首先我想到的计算算法不是什么堆栈,是简单的(其实很复杂,但是我就只需 ...

  2. 创建jsp+Servlet+JavaBean+JDBC+MySQL项目的过程

    1 根据需求建立Mysql数据,确立数据库的表的字段.属性.主键,外键等.下面我使用的数据库名dev ,表名user,字段  name,设置为主键.用户名不能为空,字段password,密码 2 在E ...

  3. javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  4. JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  5. 基于Servlet+JSP+JavaBean开发模式的用户登录注册

    http://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBea ...

  6. javaweb(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  7. JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  8. servlet+jsp+jdbc实现从数据库查询用户信息到页面

    工程创建这里就不在累述了,直接从显示User信息列表开始. 备注:我用的是servlet3的注解功能实现的,所以不需要配置web.xml 这是我的工程目录: 首先我们创建实体类: public cla ...

  9. Servlet+JSP+JDBC综合案例

    层级关系: 一.Util包 包里面写一个JDBCTools.java文件 功能:实现数据库连接返回一个Connection对象,并且可以实现数据库相应资源的关闭! 注意事项: 1.定义成员变量 1 p ...

随机推荐

  1. html 格式的email 编辑

    本篇文章只讲如何编辑html格式的email 模板,并不讲述如何用程序发送email. 1.做email的重要思想:“复古” 抛弃现代化的div+css技术,回到html4.0+table的时代.少用 ...

  2. 白板编程浅谈——Why, What, How(转)

    原文链接:http://lucida.me/blog/whiteboard-coding-demystified/ 这篇文章节选自我正在撰写的一本关于应届生面试求职的书籍,欢迎在评论或微博(@peng ...

  3. hdu1506——Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. 得到View Frustum的6飞机

    笔者:i_dovelemon 资源:CSDN 日期:2014 / 9 / 30 主题:View Frustum, Plane, View Matrix, Perspective Projection ...

  5. JDK5什么是新的线程锁技术(两)

    一个. Lock线程同步实现互斥 Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也是一个对象. 两个线程运行的代码片段要实现同步相互排斥的效果.他们必须用 ...

  6. 带项目的一些体会以及合格的 Leader 应该具备什么特质?(转)

    许多项目有这样几种 Leader: 1. 泛泛而谈型 很多时候 Leader 仅仅给出一个大方向,提一些高屋建瓴的理论方向,事情还是交由普通开发人员去做.完了可能又会回头埋怨开发人员的水平不行,没有达 ...

  7. Atitit.软件GUIbutton和仪表板(01)--警报系统--

    Atitit.软件GUIbutton和仪表板(01)--警报系统-- 1. 温度报警防区(鲁大师,360taskman) 1 2. os-区-----cpu_mem_io资源占用监測 1 3. Vm区 ...

  8. TestNg显示器(一个)-----监听器,类型和配置使用---另外META-INF详细解释

    原创文章,版权所有所有.转载,归因:http://blog.csdn.net/wanghantong/article/details/40404939 TestNg提供了听众和拦截多种接口开发我们自己 ...

  9. Tomcat通过配置一个虚拟路径管理web工程

    关于虚拟路径.学问javaweb训练课程,如今,鉴于这种情况下老师. 当我们的项目,当在不同的文件夹项目.我们如何使用tomcat去管理web工程. 教师提出的解决方案是 使用虚拟路径方式,并按照实施 ...

  10. tomcatport占用,如何识别和kill

    开始-执行-cmd,进netstat -ano你可以看到整个port入住. 增加要想知道谁占用了我们的port8080,输入下面命令 C:\Documents and Settings\Adminis ...