标签:
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设计模式
3. Model2简介
4. 阶段项目
标签:
原文地址:http://www.cnblogs.com/BlackList-Sakura/p/4521504.html