基于Myeclipse与MySQL数据库表格的增删改查(后续完善)

该博客介绍了Myeclipse与MySQL数据库表格操作的增强功能,包括退出并返回主表及登录界面带验证码的设计。新增内容涉及logoutServlet、UserServlet、image.jsp、inner.jsp、index.jsp、login.jsp、MyJsp.jsp和view.jsp的修改,以及登录界面和查看个人信息后退出的用户体验改进。
摘要由CSDN通过智能技术生成

本文是基于Myeclipse与MySQL数据库表格的增删改查后续完善部分功能的升级版

注:新增加内容:

(1)在查看完学生个人信息表后可退出并返回到主表;

(2)增加登录界面(附验证码)

一、新增信息


(1)logoutServlet.java

package com.microsofti.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import day03_inport.StudentDao;
import day03_student.Student;

public class LogoutServlet extends HttpServlet {

	private StudentDao dao = new StudentDao();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");	
		List<Student> list = dao.queryAll();
		request.setAttribute("students", list);
		//跳转回列表页面
		request.getRequestDispatcher("list.jsp").forward(request, response);	
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        this.doGet(request, response);
	}
}

(2)UserServlet.java

package com.microsofti.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取表单提交的数据
		String uname = request.getParameter("uname");
		String upwd = request.getParameter("upwd");
		
		if("lxd".equals(uname) && "123".equals(upwd)) {
			//获取是否保存用户名密码
			String daylength = request.getParameter("daylength");
			if(daylength != null && !daylength.equals("-1")){
				//保存
				int day = Integer.parseInt(daylength);
				Cookie cname = new Cookie("username", uname);
				Cookie cpwd = new Cookie("userpwd",upwd);
				//设置保存时间
				cname.setMaxAge(day*24*3600);
				cpwd.setMaxAge(day*24*3600);
				//添加到response
				response.addCookie(cname);
				response.addCookie(cpwd);
			}
			//将用户名保存在session中
			HttpSession session = request.getSession();
			session.setAttribute("currName", uname);
			//将用户名保存在request中
			request.setAttribute("abc", uname);
			//登录人数+1
			ServletContext sc = this.getServletContext();
			Integer count = (Integer)sc.getAttribute("count");
			if(count != null){
				count++;
			}else{
				count=1;
			}
			sc.setAttribute("count",count);
			//跳转到成功页面
//			response.sendRedirect("success.jsp");
			request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
		}else{
			//跳转到失败页面
			response.sendRedirect("inner.jsp
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuxiaodong1312

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值