Servlet/Jsp實現發送壓縮Web頁面 Gzip技術,servletgzip
(1)什麼話都不說,意思很簡單 就是實現頁面的壓縮後發送!據說對於篇幅比較長的頁面可以提高几百倍哦!
(2)注意事項:並不是所有的遊覽器都支援壓縮頁面的發送與接收,所以要用代碼來檢驗,如果可以則發送不可以
則按照正常的發送;
(即是:在HTTP包頭中檢查 Accept-Encoding前序,檢查他手否包含有關gzip的項,如果支援,它使用PrintWriter封
裝GZIPOutputStream,不支援的話則正常發送頁面,同時加上了一個功能 禁止頁面壓縮!)
(3)顯示頁面的servlet
package com.lc.ch04Gzip;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 LongServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out;if (GzipUtilities.isGzipSupported(request)&& !GzipUtilities.isGzipDisabled(request)) {out = GzipUtilities.getGzipWriter(response);response.setHeader("Content-Encoding", "gzip");} else {out = response.getWriter();}String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+ "Transitional//EN\">\n";String title = "Long Page";out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title+ "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n"+ "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");String line = "Bfsdfdsfdsflah, blfsdfdsfah, blasfdsdfh, blsdfdsfah, bldfsdfsdfah. "+ "Yaddsfdsdfa, ysfdsdfadda, yadsdfsdfdsda, yasdfsdfdsfdda.";for (int i = 0; i < 10000; i++) {out.println(line);}out.println("</BODY></HTML>");out.close(); // Needed for gzip; optional otherwise.}}
(4)處理壓縮的類
package com.lc.ch04Gzip;import java.io.IOException;import java.io.PrintWriter;import java.util.zip.GZIPOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class GzipUtilities { public static boolean isGzipSupported (HttpServletRequest request) { String encodings = request.getHeader("Accept-Encoding"); return((encodings != null) && (encodings.indexOf("gzip") != -1)); } public static boolean isGzipDisabled (HttpServletRequest request) { String flag = request.getParameter("disableGzip"); return((flag != null) && (!flag.equalsIgnoreCase("false"))); } public static PrintWriter getGzipWriter (HttpServletResponse response) throws IOException { return(new PrintWriter (new GZIPOutputStream (response.getOutputStream()))); }}(5)示範效果:(效果很好 不過沒有對比 不過應該可以 一般的圖片 不需要壓縮了!)
ok!
怎把一個JSP頁面的表單資料傳遞給一個servlet 要原始碼 包括(jsp、servlet、webxml)
等我給你寫一個,很簡單的。
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<%
String message ="";
if(message!=null){
out.println("aa: "+request.getParameter("aa"));
}
%>
<body>
<form action="OkServlet">
<input type="text" name="aa" id="aa"/>
<input type="submit" name="submit" id="submit" value="submit">
</form>
</body>
</html>
OkServlet.class:
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 OkServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String getaa = request.getParameter("aa");
System.out.println(getaa);
request.getRequestDispatcher("/index.jsp").forwa......餘下全文>>
用servlet&JSP技術 怎在進入indexjsp自動從資料庫取資料 頁面上不可以寫JAVA代碼
方法很多
1.先進入servlet,後轉向的index.jsp
2.自訂標籤