遇见前文的注冊模块,本篇是登录模块。主要包含登录主界面,和登录相关编写的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. directx11编程中遇到的错误及解决方法

    (2016-05-10)xnamath.h 报错: 在标识符"XMConvertToRadians"的前面 报错如下: >d:\program files\microsoft ...

  2. sails 相关文章

    Node 框架之sails   http://cnodejs.org/topic/555c3c82e684c4c8088a0ca1

  3. 查看.a架构文件

    苹果公司现在要求所有新提交的评论app,我们必须支持64位架构.而我们的在线项目编制,操作员做了一堆SDK在需要访问,我们发现,在这个过程中,有些SDK的.a文件进入后,链接错误,如提示 Undefi ...

  4. [LeetCode234]Palindrome Linked List

    题目: Given a singly linked list, determine if it is a palindrome. 判断一个单链表是不是回文 思路: 1.遍历整个链表,将链表每个节点的值 ...

  5. MVC Code First (代码优先)

    首先配置web.config <connectionStrings> <add name="BookDbContext" connectionString=&qu ...

  6. C# 读取IE缓存文件(2)

    private void button1_Click(object sender, EventArgs e) { , nBufSize; IntPtr buf; INTERNET_CACHE_ENTR ...

  7. java.io.NotSerializableException

    结果发现序列不成功非静态内部类时的序列中,出现以下异常: java.io.NotSerializableException: com.tang.sharedpreferencesdemo.MainAc ...

  8. STM32 水晶不摇

    刚刚得到一个新的董事会,该设备被编程为去,但执行地址不正确,没有进入c语言 没有进入c语言,有可能是一个难以回答的问题狗,硬狗拆除 检查以下四种情况 1.检查片内的功率是所有权利 2.检查晶体线短路 ...

  9. Swift编程语言学习1.3——类型安全和投机型

    Swift 是类型安全(type safe )语言.类型安全的语言可以让你清楚地知道代码被处理值类型.假设你需要一个代码String.你绝对不能进去一个不小心传球Int. 因为 Swift 它是类型安 ...

  10. Java于 初始化序列?

    我们正处于java于 Java中初始化的顺寻? java代码: package sru.love.c; class Person { String name = "Person"; ...