1. 程式人生 > >JavaWeb-Servlet--day01(筆記)

JavaWeb-Servlet--day01(筆記)

傳智播客-JavaWEB核心程式設計視訊教程

這裡寫圖片描述

這裡寫圖片描述

    <!-- 配置和對映Servlet -->
    <servlet>
        <!-- Servlet的註冊名 -->
        <servlet-name>MyServlet</servlet-name>
        <!-- Servlet的全類名 -->
        <servlet-class>pack_Servlet.MyServlet</servlet-class>

        <!-- 可以指定Servlet被建立的時機 -->
<load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping>

這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述

這裡寫圖片描述
XML檔案內容

<servlet>
    <servlet-name
>
MyServlet</servlet-name> <servlet-class>pack_Servlet.MyServlet</servlet-class> <!--配置初始資料--> <init-param> <param-name>name</param-name> <param-value>Micheal</param-value> </init-param> <init-param> <param-name
>
password</param-name> <param-value>[email protected]</param-value> </init-param> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping>

Servlet編寫內容

public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init()...");
        String user=servletConfig.getInitParameter("name");
        System.out.println("user"+user);

        Enumeration<String> names=servletConfig.getInitParameterNames();
        while(names.hasMoreElements()) {
            String name=names.nextElement();
            String value=servletConfig.getInitParameter(name);
            System.out.println("name:"+name+": Values:"+value);
        }
        System.out.println(servletConfig.getServletName());
    }

輸出結果:
這裡寫圖片描述

這裡寫圖片描述

XML程式碼

<context-param>
    <param-name>address</param-name>
    <param-value>NanJing</param-value>
</context-param>
<context-param>
    <param-name>age</param-name>
    <param-value>23</param-value>
</context-param>

Servlet程式碼:

ServletContext servletContext=servletConfig.getServletContext();
String address=servletContext.getInitParameter("address");
System.out.println("Address:"+address);

Enumeration<String> elements=servletContext.getInitParameterNames();
while(elements.hasMoreElements()) {
    String name=elements.nextElement();
    String value=servletContext.getInitParameter(name);
    System.out.println("name:"+name+";value:"+value);
}

這裡寫圖片描述

Servlet程式碼:

    //只能查詢WEB-INF目錄內的檔案路徑,該路徑指的是釋出在伺服器內的絕對路徑
    String path=servletContext.getRealPath("/web.xml");
    System.out.println("Path"+path);
try {
    ClassLoader classLoader=getClass().getClassLoader();
    InputStream is=classLoader.getResourceAsStream("Mytest.properties");
    System.out.println("1:"+is);
} catch (Exception e) {
    e.printStackTrace();
}

try {
    InputStream is2=servletContext.getResourceAsStream("Mytest.properties");//null,因為目錄不對
    System.out.println("2:"+is2);
    InputStream is3=servletContext.getResourceAsStream("/WEB-INF/classes/Mytest.properties");
    System.out.println("3:"+is3);
} catch (Exception e) {
    e.printStackTrace();
}

輸出結果

PathE:\Experiment\Java\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\day_01\web.xml
1:[email protected]
2:null
3:[email protected]

這裡寫圖片描述

這裡寫圖片描述

JSP介面:

<form action="/WebApplication2/NewServlet" method="post">
    Name:<input type="text" placeholder="Pelase enter name" name="name"/>
    Password:<input type="text"  placeholder="Please your password" name="password"/>
    <br/><br/>
    Hobby:
    <input type="checkbox" name="hobby" value="Music">Music
    <input type="checkbox" name="hobby" value="Sprot">Sport
    <input type="checkbox" name="hobby" value="Reading">Reading
    <input type="checkbox" name="hobby" value="Game">Game
    <br/>
    <input type="submit"  style="submit" class="login_btn" value="submit" />
</form>

Servlet介面

System.err.println("請求來了!!!");
System.err.println("request" + request);
//獲取單個引數值
String name = request.getParameter("name");
String password = request.getParameter("password");
System.err.println("name:" + name + ";password:" + password);
//獲取引數組
String hobby = request.getParameter("hobby");
System.err.println("hobby:" + hobby);
String[] hobbys = request.getParameterValues("hobby");
for (String element : hobbys) {
    System.err.print("-->" + element);
}

//獲取傳過來的引數:getParameterNames()和getParameter()
//方法一:
Enumeration<String> names=request.getParameterNames();
while(names.hasMoreElements()){
    String param_name=names.nextElement();
    String param_values=request.getParameter(param_name);//注:若是checkbox,只能獲取到首個引數
    System.err.print("^^param_name:" + param_name+";param_values"+param_values);           
}
//方法二
Map<String,String[]> map=request.getParameterMap();
for(Map.Entry<String,String[]> entry:map.entrySet()) {
    System.err.print("**param_name:" + entry.getKey()+";param_values"+Arrays.asList(entry.getValue()));
}

這裡寫圖片描述
這裡寫圖片描述
Servlet程式碼:

HttpServletRequest httpServletRequest = (HttpServletRequest) request;

String requestURI = httpServletRequest.getRequestURI();//獲取地址
System.err.println(requestURI);

String method = httpServletRequest.getMethod();//獲取請求方式
System.err.println(method);

String queryString = httpServletRequest.getQueryString();//前提:GET請求,POST請求為NULL;獲取位址列中?後面的資料
System.err.println(queryString);

String servletPath = httpServletRequest.getServletPath();//獲取當前Servlet的名稱
System.err.println(servletPath);

這裡寫圖片描述

//response.setContentType("text/html;charset=UTF-8");//預設的相應型別為html格式型別

response.setContentType("application/msword");//設定相應的型別為Word型別;

PrintWriter out = response.getWriter();//getWriter()方法
String name = request.getParameter("name");
out.println("Name:" + name);