java web學習 日常自用
請求和響應
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>
密 碼:<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);
}
}
相關文章
- Java多執行緒學習筆記(自用)Java執行緒筆記
- Java小白的學習日記一(自用筆記)Java筆記
- JAVA學習筆記—JAVA WEB(二)JAVA WEB核心(下)Java筆記Web
- Tensorflow 深度學習簡介(自用)深度學習
- Java Web 學習路線JavaWeb
- 資料庫日常遇到的需求筆記(自用)資料庫筆記
- java web視訊學習-CookieJavaWebCookie
- Java Web學習之旅開始JavaWeb
- 如何學習Java Web開發JavaWeb
- 學習Angular日常筆記Angular筆記
- java web視訊學習-TomcatJavaWebTomcat
- java web、各種框架學習地址JavaWeb框架
- Laravel 日常學習提升站點Laravel
- 學習日常:造資料 - 下
- 【JAVA Web基礎學習】Day1JavaWeb
- MyBatis-Plus日常工作學習MyBatis
- 【轉載】職場人士日常學習方法
- Java WEB學習資料,思維導圖JavaWeb
- 狂神說Java Web學習筆記_CookieJavaWeb筆記Cookie
- Docker容器學習梳理 - 日常操作總結Docker
- 狂神說Java Web學習筆記_TomcatJavaWeb筆記Tomcat
- java學習之道 --- 如何學習java?Java
- 日常學習儲存--陣列和指標陣列指標
- Web前端如何學?Web前端學習方法分享Web前端
- java-web-j2ee學習建議路線JavaWeb
- 新手Java學習路線乾貨教材 《Java Web開發實戰》JavaWeb
- CSRF&SSRF練習(自用筆記)筆記
- web前端學習——零基礎web前端學習路線Web前端
- web知識學習Web
- web前端學習之路Web前端
- web前端學習方案Web前端
- Web開發學習Web
- web前端學習指南Web前端
- Web前端學習——JavaScriptWeb前端JavaScript
- Web前端學習——HTMLWeb前端HTML
- Web前端學習——CSSWeb前端CSS
- Web【轉載學習】Web
- 【Web】Web 階段學習筆記Web筆記