Java Web總結

1、地址css

客戶端路徑和服務端路徑html

表單:<form action="路徑"></form>java

超連接:<a href="路徑">xxx</a>web

圖片:<img src="路徑"/>數組

重定向:response.sendRedirect("路徑")瀏覽器

請求轉發:request.getQuestDispatcher("路徑").forward(request,response)tomcat

請求包含:request.getQuestDisPatcher("路徑").include(request,response);服務器

ServletContext 獲取資源的相關方法jsp

servletContext.getRealPath("路徑")//根據文件的相對路徑獲取絕對路徑post

servletContext.getResourceAsStream("路徑")//根據文件的相對路徑獲取文件的輸入流

在web.xml文件中配置的Servlet的訪問路徑

<url-pattern>路徑</url-pattern>

客戶端路徑:客戶端訪問服務器資源的時候的路徑 

一、表單

在程序中的書寫方式:http://127.0.0.1:8888/day11/AServlet

服務器主機名/端口號/項目名/資源名

二、超連接

<a href="路徑">xxx</a>

三、圖片

<img src="/day11/images/stu,jpg"/>

4.重定向

response.sendRedirect("/day11/AServlet");

服務端路徑:

一、請求轉發

二、請求包含

三、在ServletContext中與資源相關的方法

  servletContext.getRealPath("/WEB-INF/web.xml")//根據文件的相對路徑獲取絕對路徑

四、在web.xml文件中配置的Servlet的訪問路徑

<url-pattern>/AServlet</url-pattern>

2、編碼

從客戶端、瀏覽器向服務器發送請求數據或從服務器向客戶端發送響應數據,在底層都是以字節的形式發送的,這就須要將字符數據轉換成字節,這個過程叫作編碼。

在客戶端、瀏覽器或者服務器端接收到字節數據後,須要把字節組裝成字符,這個過程叫作解碼。

不管編碼和解碼都須要用到碼錶。經常使用的編碼解碼方式有GBK gb2312 ISO-8859-1

服務器默認的編碼和解碼都是ISO-8859-1 

產生亂碼的根源是編碼和解碼的方式不一致,只要編碼和解碼的方式一致就不會產生亂碼。

3、請求編碼

(1)在頁面中發出請求

用戶在頁面上輸入的數據的編碼是由頁面自己的編碼決定的

(2)get請求解碼

服務器默認的解碼方式爲ISO-8859-1 ,不支持中文。

解決亂碼須要兩步:一是回退爲字節數據 二是從新編碼

AServlet.java

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;
import java.util.ArrayList;

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

public class AServlet extends HttpServlet {
    /*
     * Post請求
     *
     */
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         /*post請求的解碼處理   亂碼出現的根源是:編碼方式和解碼方式不一致。
          * tomcat服務器默認的解碼方式是ISO-8859-1不支持中文。因爲post請求
          * 有請求體,請求參數是放在請求體中的,設置請求體的解碼方式,須要調用
          * 方法request.setCharacterEncoding("UTF-8");支持中文
          */
        request.setCharacterEncoding("UTF-8");
        //響應輸出流的編碼方式是UTF-8 客戶端、瀏覽器解碼方式也爲UTF-8
        response.setContentType("text/html;charset=utf-8");
        //根據參數名獲取參數值
        String username = request.getParameter("username");
        String sex = request.getParameter("sex");
        String select = request.getParameter("select");
        //根據參數名獲取多個參數值
        String[] hobby = request.getParameterValues("hobby");
        String experiment = request.getParameter("experiment");
        //輸入到界面上
        response.getWriter().print("姓名:"+username+"<br>");
        response.getWriter().print("性別:"+sex+"<br>");
        response.getWriter().print("學歷:"+select+"<br>");
        //建立一個數組用於存放愛好的值
        ArrayList hobbies = new ArrayList();
        //數組的遍歷
        for (int i = 0; i < hobby.length; i++) {
            hobbies.add(hobby[i]);
        }
        response.getWriter().print("愛好:"+hobbies+"<br>");
        response.getWriter().print("工做經歷:"+experiment+"<br>");
    }

}

a.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
    <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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="/day11/AServlet" method="post">
        姓名:<input type="text" name="username"><br>
        性別:<input type="radio" name="sex" value="male"><input type="radio" name="sex" value="female"><br>
        學歷:<select name="select">
                <option selected="selected" value="college">本科</option>
                <option value="junior">大專</option>
                <option value="master">碩士</option>
                <option value="doctor">博士</option>
            </select><br>
        愛好:<input type="checkbox" name="hobby" value="sing">唱歌
            <input type="checkbox" name="hobby" value="dance">跳舞
            <input type="checkbox" name="hobby" value="read">閱讀
            <input type="checkbox" name="hobby" value="run">跑步<br>
        工做經歷:<br>
            <textarea name="experiment" rows="5" cols="35">請認真寫下您的工做經歷</textarea><br>
            <input type="submit" value="提交">
    </form>
  </body>
</html>
相關文章
相關標籤/搜索