java web學習 日常自用

火眼猊發表於2020-09-28

請求和響應

Servlet最主要的作用是用來處理客戶機的請求,並向客戶做出響應。因此Web伺服器再呼叫Service()方法之前都會建立HTTPServletRequest和HttpServletResponse兩個物件。

上圖為瀏覽器訪問Servlet的過程
1.SetStatus(int status)方法
用於設定HTTP響應訊息的狀態碼,生成響應狀態行。正常情況下,會產生一個預設的狀態碼200的狀態行
2.SendError(int sc)方法
用於傳送表示錯誤的狀態碼。
3.傳送相應訊息頭相關的方法
可以點開這裡看一下 他的描述還是很全面的

下面的程式碼我用來自己看的
Test1

package cn.itcast.servlet;

import java.io.IOException;
import java.io.OutputStream;

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

/**
 * Servlet implementation class Test1
 */
public class Test1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String data="itcast";
		//獲取位元組輸出物件
		OutputStream out = response.getOutputStream();
		out.write(data.getBytes());//輸出資訊
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Test2

package cn.itcast.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;

/**
 * Servlet implementation class Test3
 */
public class Test3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String data="fire";
		//獲取字元輸出流物件
		PrintWriter print = response.getWriter();
		 print.write(data);//輸出資訊
		 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Test3

package cn.itcast.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;

/**
 * Servlet implementation class Test4
 */
public class Test4 extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 設定字元編碼
		response.setContentType("text/html;charset=utf-8");
		String data="中國";
		PrintWriter out=response.getWriter();
		out.println(data);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Test4

package cn.itcast.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test5
 */
public class Test5 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 兩秒後重新整理並跳轉到傳智播客官網首頁
		response.setHeader("Refresh","2;URL=http://www.itcast.cn");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Test5

package cn.itcast.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test6
 */
public class Test6 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//每隔三秒定時重新整理當前頁面
		response.setHeader("Refresh","3");
		response.getWriter().print(new java.util.Date());//輸出當前時間
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

實現網頁定時重新整理並跳轉的登入頁面*
首先login.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Trasitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/chapter/LoginServlet" method="post">
使用者名稱:<input type="text" name="username"/><br>
密&nbsp;&nbsp;&nbsp;碼:<input type="password" name="password"/><br>
<input type="submit" value="登入"/>
	</form>
</body>
</html>

接下來是登入成功的頁面

<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html ; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
歡迎你,登陸成功!
</body>
</html>

最後Servlet

package cn.itcast.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html,charset=utf-8");
		//用HttpServletResquest物件的getParameter()方法獲取使用者名稱和密碼
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		System.out.println("name:"+username);
		//假設使用者名稱和密碼分別為:itcast和123
		if (("itcast").equals(username)&&("123").equals(password)) {
			//如果使用者名稱和密碼正確,重定向到welcome.html
			response.sendRedirect("/chapter/welcome.html");
		}else{
			//如果使用者名稱和密碼不正確,重定向到login.html
			response.sendRedirect("/chapter/login.html");
		}
			
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

相關文章