JavaWeb编写登录注册案例并把数据插入MySQL数据库中

小白学习了这么久的java,第一次上手编写一个完整的登录以及注册案例,麻雀虽小五脏俱全!!!!

案例:

登录和注册

第一:所需创建的包以及相关类

1,domain包(也就是平时所说的Javabean),类:User——提供get/set方法。

2,servicer包,类:UserServer类——与用户相关的业务类。

3,dao包,类UserDao——与与用户相关的数据类。

4,servlet包,类: LoginServlet和RegistServlet

5,JSP页面:     login.jsp  --> 登录表单
                               regist.jsp --> 注册表单
                                index.jsp -->  主页(只有登录成功才能看到)

第二:搭建空项目

1,domain包的类User:

package domain;
/*
 * 实体类
 * */
public class User {
	private String userName;
	private String passWord;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	@Override
	public String toString() {
		return "User [userName=" + userName + ", passWord=" + passWord + "]";
	}
}

2,servicer包类:UserServer

package Server;

import domain.User;
/*user的业务层
 * 
 * */
public class UserServer {
	 private User UserServer =new User();
}

3,dao包类UserDao

package dao;

/*
 * 访问数据库
 * */
public class UserDao {
	
}

4,servlet包,类: LoginServlet和RegistServlet

package Servlet;

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

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

import Server.UserServer;
/*
 * userServlet层
 * */
public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		UserServer userservlet =new UserServer();
	}

}
package Servlet;

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

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

import Server.UserServer;

public class RegistServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		UserServer userservlet =new UserServer();
	}

}

5,jsp页面:

Login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="0" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	

  </head>
  
  <body>
   <form method="post" action="action=" <c:url value='/LoginServlet'/>"">
   	用户名:<input type="text" name="username"><br>
   	密码:<input type="password" name="password"><br>
   	<input type="submit" value="提交">
   	<input type="reset" value="重置">
   	<a href="regist.jsp">点击注册</a>
   </form>
  </body>
</html>

regist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="0" uri="http://java.sun.com/jsp/jstl/core"%>

  <head>

    
    <title>My JSP 'regist.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	

  </head>
  
  <body>
     <form method="post" action=" <c:url value='/RegistServlet'/>">
   	用户名:<input type="text" name="username"><br>
   	密码:<input type="password" name="password"><br>
   	确认密码:<input type="password" name="password"><br>
   	<input type="submit" value="提交">
   	<input type="reset" value="重置">
   </form>
  </body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    欢迎登陆到任性的代码的博客<a href="https://blog.csdn.net/qq_31539817"></a><br>
  </body>
</html>

 

第三:数据库设计:

mysql> create database USER;
mysql> CREATE TABLE USERINFO
    -> (USERNAME VARCHAR(50),
    -> PASSWORD VARCHAR(50));
mysql> SHOW TABLES;
+----------------+
| Tables_in_user |
+----------------+
| userinfo       |
+----------------+
1 row in set (0.00 sec)

mysql> DESC UESRINFO
    -> ;
ERROR 1146 (42S02): Table 'user.uesrinfo' doesn't exist
mysql> DESC USERINFO;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| USERNAME | varchar(50) | YES  |     | NULL    |       |
| PASSWORD | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql>

第四:编写代码

资源包:https://download.csdn.net/download/qq_31539817/11445397

第五:说明

很多逻辑业务没用做,比如判断用户名是否重复,用户和密码是否符合规范,没有加样式等,这些功能可以自己添加。如果没有c币下载可以留言(csdn真尼玛币坑,c币还会过期,我的全部c币都过期了,坑人的傻逼网站)。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章