小白學習了這麼久的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幣都過期了,坑人的傻逼網站)。