1.寫網頁有html,jsp,servlet.但是三者有各自的優缺點,html適合寫一些靜態的顯示,jsp適合寫動態可變的顯示,servlet適合處理業務邏輯,分發轉向,DAO數據中轉。
2.本專案使用jsp+servlet來實現一個簡單的網頁註冊和登錄,需要一點jsp的語法基礎,例如<% 插入java程式碼%>、<a herf= ""></a>
表示轉送。 form跟html一樣表示表單。 <!---- ----->
表示註解。
<%! %>申明全域變數等基礎的語法,還有幾個指令page include taglib
3.建立一個web網頁,預設產生一個web.xml檔。工程名稱為webServletTest 在java Resouces目錄下建立一個webservlet.java。
package webJspDemo.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;/** * Servlet implementation class LoginServlet */@WebServlet("/servlet/loginservlet")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()); //获取表单数据 response.setContentType("text/html; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); String userName = request.getParameter("userName"); String pwd = request.getParameter("pwd"); if("tom".equals(userName)&&"123".equals(pwd)){ request.getSession().setAttribute("name", userName); request.getRequestDispatcher("/register.jsp").forward(request, response); }else{ request.setAttribute("msg", "用户名或者密码不正确"); request.getRequestDispatcher("/login.jsp").forward(request, response); } //分发转向 } /** * @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); } }
4.修改web.xml的映射關係與類別名稱
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>webJspDemo.com.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
5.在WEB-INF目錄下建立3個jsp,分別為home.jsp、login.jsp、register .jsp檔。
home.jsp程式碼為
<body> <h1>欢迎来到本站!</h1> <% String userName =(String)session.getAttribute("name"); out.print(userName); %> </body>
login.jsp程式碼為:
<body> <% String msg = (String)request.getAttribute("msg"); if(msg !=null){ out.print(msg); } %> <form action="/webJspDemo1/servlet/loginservlet" method="get"> 用户名:<input type="text" name="userName"/><br/> 密码:<input type="password" name="pwd"/><br/> <input type="submit" value="登录"/><br/> </form></body>
register.jsp程式碼為:
<body> 欢迎你登录: <% String userName =(String)session.getAttribute("name"); out.print(userName); %> <a href="/webJspDemo1/home.jsp">跳到主页 </a></body>
5.實驗結果
在瀏覽器中輸入:http://localhost:8080/webJspDemo1/login
在表單中輸入tom和123之後就會跳到register.jsp介面,然後在到home.jsp介面,基本上就完成一個簡單的註冊流程。
以上是用Java實現網頁登入和註冊的詳細內容。更多資訊請關注PHP中文網其他相關文章!