<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
環境準備:
本文所用到環境如下:
軟體:Eclipse(2018)
伺服器:Tomcat 9
在index.jsp新增相關的程式碼
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>使用者註冊</h1> <div> <form action="RegisterServlet" method="get"> <table> <tr> <td>登入名:</td> <td><input type="text" name="username" required> (可包含a-z、0-9和下劃線)</td> </br> <tr> <td>密碼:</td> <td><input type="password" name="password" required> (至少包含6個字元)</td> </tr> <tr> <td>再次輸入密碼:</td> <td><input type="password" name="confirmPassword" required></td> </tr> <tr> <td>電子郵箱:</td> <td><input type="text" id="email" name="youxaing" required> (必須包含@字元)</td> </tr> <tr> <td>性別:</td> <td><input type="radio" name="gender" value="男" required>男 <input type="radio" name="gender" value="女" required>女</td> </tr> <td>頭像:</td> <td><input type="file" name="bfile" value="選擇檔案" accept="image/*" ></td> </tr> <td>愛好:</td> <td> <p> <input type="checkbox" name="hobby" value="運動"> 運動 <input type="checkbox" name="hobby" value="聊天" > 聊天 <input type="checkbox" name="hobby" value="玩遊戲" > 玩遊戲</p> </td> </tr> </tr> <td>喜歡的城市:</td> <td> <select name="selectList"> <option>[請選擇]</option> <option>北海</option> <option>海南</option> <option>重慶</option> <option>杭州</option> <option>深圳</option> <option>成都</option> </select> </tr> </table> <div> <p> <input type="submit" value="提交"> <input type="reset" value="重置"> </p> </div> </form> </div> </body> </html>
效果為:
Login_Servlet.java程式碼
package onlyxiu_ceshi.com; 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 javax.servlet.http.HttpSession; /** * Servlet implementation class Login_Servlet */ @WebServlet("/Login_Servlet") public class Login_Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Login_Servlet() { 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()); request.setCharacterEncoding("UTF-8");//處理亂碼問題 response.setCharacterEncoding("UTF-8");//處理亂碼問題 //獲得使用者在register介面提交的資料 String username = request.getParameter("username"); String password = request.getParameter("password"); String confirmPassword = request.getParameter("confirmPassword"); String gender = request.getParameter("gender"); String youxaing = request.getParameter("youxaing"); String bfile = request.getParameter("bfile"); String hobby = request.getParameter("hobby"); String selectList = request.getParameter("selectList"); //判斷密碼是否正確 if (password.equals(confirmPassword)){//如果密碼相同,重定向到成功介面 HttpSession session = request.getSession();//獲取session session.setAttribute("session_username",username); session.setAttribute("session_password",password); session.setAttribute("session_gender",gender); session.setAttribute("session_youxaing",youxaing); session.setAttribute("session_bfile",bfile); session.setAttribute("session_hobby", hobby); session.setAttribute("session_selectList",selectList); request.getRequestDispatcher("SuccessServlet").forward(request, response); // response.sendRedirect("register_login.jsp");//重定向到成功頁面 }else {//如果密碼不同,通知使用者密碼輸入不一樣 request.getSession().setAttribute("passwordError","yes"); request.getRequestDispatcher("ErrorServlet").forward(request, response); // response.sendRedirect("register.jsp");//重定向到登陸介面 } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
設定web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>onlyxiu_ceshi</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Login_Servlet</servlet-name> <servlet-class>onlyxiu_ceshi.com</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login_Servlet</servlet-name> <url-pattern>/onlyxiu_ceshi/Login_Servlet</url-pattern> </servlet-mapping> </web-app>
ErrorServlet.java的程式碼
package onlyxiu_ceshi.com; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; 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 javax.servlet.http.HttpSession; /** * Servlet implementation class ErrorServlet */ @WebServlet("/ErrorServlet") public class ErrorServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ErrorServlet() { 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()); response.setHeader("Content-type","text/html; charset=UTF-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String gender = request.getParameter("gender"); String youxaing = request.getParameter("youxaing"); String bfile = request.getParameter("bfile"); String hobby = request.getParameter("hobby"); String selectList = request.getParameter("selectList"); PrintWriter out = response.getWriter(); out.print("<h1>使用者註冊資訊</h1>"); out.print("使用者名稱:n"+username+"<br>"); out.print("密碼:"+password+"<br>"); out.print("電子郵箱:"+youxaing+"<br>"); out.print("性別:"+gender+"<br>"); out.print("頭像:"+bfile+"<br>"); out.print("愛好:"+hobby+"<br>"); out.print("喜歡的城市:"+selectList+"<br>"); // out.print("登陸失敗"); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // doGet(request, response); } }
SuccessServlet.java
package onlyxiu_ceshi.com; import java.io.IOException; import java.io.PrintWriter; 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 javax.servlet.http.HttpSession; /** * Servlet implementation class SuccessServlet */ @WebServlet("/SuccessServlet") public class SuccessServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SuccessServlet() { 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()); response.setHeader("Content-type","text/html; charset=UTF-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String gender = request.getParameter("gender"); String youxaing = request.getParameter("youxaing"); String bfile = request.getParameter("bfile"); String hobby = request.getParameter("hobby"); String selectList = request.getParameter("selectList"); PrintWriter out = response.getWriter(); out.print("<h1>使用者註冊資訊</h1>"); out.print("使用者名稱:n"+username+"<br>"); out.print("密碼:"+password+"<br>"); out.print("電子郵箱:"+youxaing+"<br>"); out.print("性別:"+gender+"<br>"); out.print("頭像:"+bfile+"<br>"); out.print("愛好:"+hobby+"<br>"); out.print("喜歡的城市:"+selectList+"<br>"); // out.println("登陸成功"); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // doGet(request, response); } }
到此這篇關於JavaWeb之Servlet註冊頁面的實現範例的文章就介紹到這了,更多相關Servlet註冊頁面內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45