時間:2023-09-21 09:12:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-09-21 09:12:01 來源:網(wǎng)站運(yùn)營
大數(shù)據(jù)從入門到深入:JavaEE 之 動態(tài)網(wǎng)頁開發(fā)基礎(chǔ) JSP實戰(zhàn)優(yōu)化:Servlet基礎(chǔ):<%@ 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>用戶登錄</title></head><body> <div> <h1>servlet小案例:用戶登錄</h1> <form action="userlogin" method="post"> <p> 請輸入您的用戶名 :<input type="text" name="loginName" /> </p> <p> 請輸入您的密碼 : <input type="password" name="loginPass" /> </p> <p> <button type="submit">登錄</button> </p> </form> </div></body></html>
登錄成功: <%@ 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>登錄成功</title></head><body> <div> <marquee> <h1>恭喜,登錄成功!</h1> </marquee> </div></body></html>
登錄失敗:<%@ 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>登錄失敗</title></head><body> <div> <marquee> <h1>很遺憾,登錄失敗!</h1> </marquee> </div></body></html>
2) 創(chuàng)建Servlet package com.hnxy.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class UserLoginAction11 */public class UserLoginAction extends HttpServlet { private static final long serialVersionUID = 1L; /** * @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 doGet(request, response); }}
稍加修改就可以使用了package com.hnxy.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 用戶登錄的控制器 * @author My * */public class UserLoginAction extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設(shè)定編碼 request.setCharacterEncoding("UTF-8"); // 獲取頁面數(shù)據(jù) String loginName = request.getParameter("loginName"); String loginPass = request.getParameter("loginPass"); // 業(yè)務(wù)判斷 if("admin".equals(loginName)&&"admin".equals(loginPass)){ request.getRequestDispatcher("success.jsp").forward(request, response); }else{ response.sendRedirect("error.jsp"); } }}
3) 調(diào)試運(yùn)行 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>javaweb_class3_tp1</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> <description></description> <display-name>UserLoginAction</display-name> <servlet-name>UserLoginAction</servlet-name> <servlet-class>com.hnxy.controller.UserLoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginAction</servlet-name> <url-pattern>/userlogin</url-pattern> </servlet-mapping></web-app>
簡單了解一下什么是XML? <!-- webapp的項目名稱 --><display-name>javaweb_class3_tp1</display-name>
<!-- webapp的歡迎頁面 也就是說如果訪問 http://127.0.0.1:8080/javaweb_class3_tp1 沒有指定要訪問的JSP的話 tomcat會默認(rèn)跳轉(zhuǎn)到下面存在的一個頁面中去 --> <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> <description></description> <display-name>UserLoginAction</display-name> <servlet-name>UserLoginAction</servlet-name> <servlet-class>com.hnxy.controller.UserLoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginAction</servlet-name> <url-pattern>/userlogin</url-pattern> </servlet-mapping>
這個是我們UserLoginAction這個Servlet的核心配置部分,不知道大家還有沒有印象剛才我們 通過頁面請求已經(jīng)分析到 <servlet> <description></description> <display-name>UserLoginAction</display-name> <servlet-name>UserLoginAction</servlet-name> <servlet-class>com.hnxy.controller.UserLoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserLoginAction</servlet-name> <url-pattern>/userlogin</url-pattern> </servlet-mapping>
每個servlet都有一個這個的配置,需要注意的是: 關(guān)鍵詞:基礎(chǔ),實戰(zhàn),入門,數(shù)據(jù),深入,動態(tài)
客戶&案例
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。