javaWeb之servlet获取初始化参数与MVC简介

javaWeb之servlet获取初始化参数与MVC简介

javaWeb之servlet获取初始化参数与MVC简介_第1张图片

index.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting pagetitle>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
  head>

  <body>
      <h1>获取初始化参数演示案例h1>
      <hr>
      <a href="servlet/GetInitParameterServlet">获取初始化参数Servleta>
  body>
html>

web.xml:


<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletGetInitParameterDemodisplay-name>
  <servlet>
    <description>This is the description of my J2EE componentdescription>
    <display-name>This is the display name of my J2EE componentdisplay-name>
    <servlet-name>GetInitParameterServletservlet-name>
    <servlet-class>servlet.GetInitParameterServletservlet-class>
    <init-param>
        <param-name>usernameparam-name>
        <param-value>adminparam-value>
    init-param>
    <init-param>
        <param-name>passwordparam-name>
        <param-value>123456param-value>
    init-param>
  servlet>

  <servlet-mapping>
    <servlet-name>GetInitParameterServletservlet-name>
    <url-pattern>/servlet/GetInitParameterServleturl-pattern>
  servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
web-app>

GetInitParameterServlet.java:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetInitParameterServlet extends HttpServlet {

    private String username;//用户名
    private String password;//密码


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    /**
     * Constructor of the object.
     */
    public GetInitParameterServlet() {
        super();
    }

    /**
     * Destruction of the servlet. 
*/
public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.println("

"+"用户名:"+this.getUsername()+"

"
); out.println("

"+"密码:"+this.getPassword()+"

"
); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */
public void init() throws ServletException { // Put your code here this.setUsername(this.getInitParameter("username")); this.setPassword(this.getInitParameter("password")); } }

注:ServletConfig可以获得Servlet的初始化参数

补充,MVC设计模式:

javaWeb之servlet获取初始化参数与MVC简介_第2张图片

model1与model2的对比:
javaWeb之servlet获取初始化参数与MVC简介_第3张图片
javaWeb之servlet获取初始化参数与MVC简介_第4张图片

你可能感兴趣的:(JavaWeb,servlet,java,web,servlet)