码迷,mamicode.com
首页 > 编程语言 > 详细

JAVAWEB 一一 userweb2(升级,servlet版,jstl和el)

时间:2017-11-09 20:56:42      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:request   error   forward   过滤   x64   占位符   exception   patch   tno   

 创建数据库和表

 

首先,创建一个web项目

技术分享

 

然后引入jar包(jstl.jar和standard.jar是jstl和el包,在jsp页面中需要手动加 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 才有效)

技术分享

 

创建jsp页面(doXX.jsp的代码全部转移到servlet里面)

技术分享

 

创建包

技术分享

 

 

创建接口

技术分享

实现类

技术分享

创建servlet

技术分享

创建fiter过滤器

技术分享

创建工具类(分页)

技术分享

 

详细内容

首先创建一个登陆页面 login.jsp

  

 

创建要跳转的页面 (充当半个控制器servlet)

 dologin.jsp

将dologin.jsp的代码复制粘贴到

UserServlet.java

package com.user.servlet.user;

import java.io.IOException;


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

import com.user.service.UserService;
import com.user.service.impl.UserServiceImpl;

public class UserServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		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 {
	   	//设置前台页面参数编码格式
    	request.setCharacterEncoding("UTF-8");
    	//获取前台页面参数
    	String username = request.getParameter("username");
    	String password = request.getParameter("password3");
    	//调用Sevice层方法  判断是否登陆成功
    	UserService service = new UserServiceImpl();
    	boolean isLogin = service.isLogin(username, password);
    	//如果登陆成功则跳转至success.jsp,否则跳转至login.jsp重新登陆
    	if(isLogin){
    	//转发
    		request.getRequestDispatcher("EmpServlet").forward(request, response);
    	}else{
    	 //重定向
    		response.sendRedirect("jsp/login.jsp");
    	}

	}

	/**
	 * 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 {
		doGet(request,response);

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

  

 成功跳转

  success.jsp

  

 

  否则

   Login.jsp

  

 User

entity层

 

 技术分享

实体类

User.java

get set 方法 有参无参构造器

  

service层

接口 UserService.java

  

实现类 UserServiceImpl.java

 

dao层

接口 UserDao.java

  

实现类 UserDaoImpl.java

 

dao层  

basedao,java

Login.jsp

  

dologin.jsp

将dologin.jsp的代码复制粘贴到

UserServlet.java

package com.user.servlet.user;

import java.io.IOException;


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

import com.user.service.UserService;
import com.user.service.impl.UserServiceImpl;

public class UserServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		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 {
	   	//设置前台页面参数编码格式
    	request.setCharacterEncoding("UTF-8");
    	//获取前台页面参数
    	String username = request.getParameter("username");
    	String password = request.getParameter("password3");
    	//调用Sevice层方法  判断是否登陆成功
    	UserService service = new UserServiceImpl();
    	boolean isLogin = service.isLogin(username, password);
    	//如果登陆成功则跳转至success.jsp,否则跳转至login.jsp重新登陆
    	if(isLogin){
    	//转发
    		request.getRequestDispatcher("EmpServlet").forward(request, response);
    	}else{
    	 //重定向
    		response.sendRedirect("jsp/login.jsp");
    	}

	}

	/**
	 * 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 {
		doGet(request,response);

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

  

success .jsp

  

 也可以直接跳转到别的页面比如list.jsp 一个雇员信息列表的页面 但是  这个页面的数据是从数据库查出来的(这样才是动态页面啊)

那么就需要再做一遍上面的步骤  (创建Emp接口和实现类 还有dolist页面 list页面)

 


Emp

entity层

Emp.java

  

service层

接口 EmpService.java

  

实现类 EmpServiceImpl

  

dao层

接口 EmpDao.java

  

实现类 EmpDaoImpl.java

  

 查

dolist.jsp

这个页面的数据是从数据库查出来的(这样才是动态页面啊)

所以 我们需要跳转到一个dolist页面 (充当servlet)让他把页面从数据来出来后跳转到list页面

  

list.jsp

  

 

 

增:

在list展示页面 a标签对应的一个添加雇员的页面

技术分享

 

 

addEmp.jsp

  

 

doadd .jsp

  

  

dolist.jsp

  

list.jsp

  

list.jsp页面

添加一个 删除连接

技术分享

dodel.jsp

  dolist.jsp

  list.jsp

  

 

 改

 改的话  多一步 因为 你要先根据条件从数据库查出你要改的那条记录

然后用一个页面接收 然后修改 提交 然后 再查一下表

 list.jsp页面中添加一个连接

 

技术分享

dopreupdate.jsp

  

preupdate.jsp

  doupdate.jsp

  dolist.jsp

  list.jsp

 登录

Login.jsp dologin.jsp

  成功                                                                dolist.jsp list.jsp

  失败   Login.jsp

查 dolist.jsp list.jsp

增 addEmp.jsp doadd.jsp                                             dolist.jsp list.jsp

删 dodel.jsp                                                                  dolist.jsp list.jsp

改 dopreupdate.jsp preupdate.jsp doupdate.jsp          dolist.jsp list.jsp

JAVAWEB 一一 userweb2(升级,servlet版,jstl和el)

标签:request   error   forward   过滤   x64   占位符   exception   patch   tno   

原文地址:http://www.cnblogs.com/PoeticalJustice/p/7811014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!