在eclipse中建立了web项目,实现注册和登录还有在个人中心显示用户名密码
注册功能
源码如下
package com.sevlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.demo.DBUtil;
/**
* Servlet implementation class RegisterSevlet
*/
@WebServlet("/Register_do")
public class RegisterSevlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterSevlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/*protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username=request.getParameter("username");
String password=request.getParameter("password");
int age=Integer.parseInt(request.getParameter("age"));
String sex=new String(request.getParameter("sex").getBytes("iso-8859-1"),"utf-8");
boolean isSuccess=DBUtil.addUser(username, password, age, sex);
if(isSuccess){
request.setAttribute("message", "注册成功!请登录");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
//out.println("注册失败,用户名被占用");
request.setAttribute("message", "注册失败,用户名重复");
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
}
登录功能
源码如下
package com.sevlet.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.demo.DBUtil;
import com.web.demo.User;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login_do")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// // TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
// }
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username=request.getParameter("username");
String password=request.getParameter("password");
User user=DBUtil.verifyAccount(username, password);
if(user==null){
//out.println("登陆失败");
request.setAttribute("message", "<font color='red'>登陆失败,用户名或密码错误</font>");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
//out.println("登陆成功");
//request.setAttribute("user", user);
request.getSession().setAttribute("user", user);
request.getRequestDispatcher("personCenter.jsp").forward(request, response);
}
}
}
个人中心
我使用jsp实现
源码如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="com.web.demo.User"%>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>personCenter</title>
</head>
<body>
<%
User user=(User)session.getAttribute("user");
%>
用户名:<%=user.getUsername() %><br/>
密码:<%=user.getPassword() %><br/>
性别:<%=user.getSex() %><br/>
年龄:<%=user.getAge() %>
</body>
</html>
实现效果如下图