日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Servlet高级

發布時間:2025/5/22 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Servlet高级 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 獲取初始化參數

在web.xml中配置Servlet時,可以配置一些初始化參數。而在Servlet中可以通過ServletConfig接口提供的方法來取得這些參數。

index.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <h1>獲取初始化參數演示案例</h1> 25 <hr> 26 <a href = "servlet/GetInitParam">獲取初始化參數</a> 27 </body> 28 </html>

web.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <display-name></display-name> 8 <servlet> 9 <servlet-name>GetInitParam</servlet-name> 10 <servlet-class>servlet.GetInitParam</servlet-class> 11 <init-param> 12 <param-name>username</param-name> 13 <param-value>admin</param-value> 14 </init-param> 15 <init-param> 16 <param-name>password</param-name> 17 <param-value>123456</param-value> 18 </init-param> 19 </servlet> 20 21 <servlet-mapping> 22 <servlet-name>GetInitParam</servlet-name> 23 <url-pattern>/servlet/GetInitParam</url-pattern> 24 </servlet-mapping> 25 <welcome-file-list> 26 <welcome-file>index.jsp</welcome-file> 27 </welcome-file-list> 28 </web-app>

GetInitParam.java

1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class GetInitParam extends HttpServlet { 12 13 private String username; // 用戶名 14 private String password; // 密碼 15 16 public String getUsername() { 17 return username; 18 } 19 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 24 public String getPassword() { 25 return password; 26 } 27 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 32 /** 33 * The doGet method of the servlet. <br> 34 * 35 * This method is called when a form has its tag value method equals to get. 36 * 37 * @param request the request send by the client to the server 38 * @param response the response send by the server to the client 39 * @throws ServletException if an error occurred 40 * @throws IOException if an error occurred 41 */ 42 public void doGet(HttpServletRequest request, HttpServletResponse response) 43 throws ServletException, IOException { 44 45 doPost(request, response); 46 } 47 48 /** 49 * The doPost method of the servlet. <br> 50 * 51 * This method is called when a form has its tag value method equals to post. 52 * 53 * @param request the request send by the client to the server 54 * @param response the response send by the server to the client 55 * @throws ServletException if an error occurred 56 * @throws IOException if an error occurred 57 */ 58 public void doPost(HttpServletRequest request, HttpServletResponse response) 59 throws ServletException, IOException { 60 61 response.setContentType("text/html; charset=UTF-8"); 62 PrintWriter out = response.getWriter(); 63 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 64 out.println("<HTML>"); 65 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 66 out.println(" <BODY>"); 67 out.println(" <h2>賬號:" + this.getUsername() + "</h2>"); 68 out.println(" <h2>密碼:" + this.getPassword() + "</h2>"); 69 out.println(" </BODY>"); 70 out.println("</HTML>"); 71 out.flush(); 72 out.close(); 73 } 74 75 public void init() throws ServletException { 76 this.setUsername(this.getInitParameter("username")); 77 this.setPassword(this.getInitParameter("password")); 78 } 79 }

?

2. MVC設計模式

MVC模式:MVC(model、view、controller),是軟件開發過程中比較流行的設計思想。旨在分離模型、控制、視圖。是一種分層思想的體現。

?

3. Model2簡介

?

?

4. 階段項目

使用Servlet技術實現購物車效果。

?

轉載于:https://www.cnblogs.com/BlackList-Sakura/p/4521504.html

總結

以上是生活随笔為你收集整理的Servlet高级的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。