Java web:jsp,個人注意事項,創(chuàng)建servlet,HTTP協(xié)議,學(xué)習(xí)筆記PPT【詩書畫唱】
1、創(chuàng)建一個servlet,調(diào)用這個servlet以后要求控制臺打印出服務(wù)器的時間SimpleDateFormat轉(zhuǎn)換打印出2000-9-16 9:13:16。(個人的注意事項:用快捷方式生成的servlet,不必修改web.xml【自己手建的servlet,要修改web.xml】)

package JSP;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class SimpleDateFormat
?*/
@WebServlet("/s")
public class NowTime extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public NowTime() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request,?
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest?
request, HttpServletResponse response)?
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest?
* request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Date date=new Date();?
//這個方法也是需要導(dǎo)包的?
//注意第二個mm要大寫,不然月份會有錯誤?
SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd hh:mm:ss");?
System.out.println(sdf.format(date));?
}
}




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
? <display-name>ZCJSP</display-name>
? <welcome-file-list>
? ? <welcome-file>index.html</welcome-file>
? ? <welcome-file>index.htm</welcome-file>
? ? <welcome-file>index.jsp</welcome-file>
? ? <welcome-file>default.html</welcome-file>
? ? <welcome-file>default.htm</welcome-file>
? ? <welcome-file>default.jsp</welcome-file>
? </welcome-file-list>
</web-app>

2、創(chuàng)建一個servlet,調(diào)用這個servelt以后要求控制臺打印出一個隨機數(shù)。

package JSP;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class Random
?*/
@WebServlet("/Random")
public class RandomNum extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public RandomNum() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest
*? request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response)?
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest?
* request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
Random R=new Random();
int num=R.nextInt(666);
System.out.println(num);
}
}




3、創(chuàng)建一個servlet,調(diào)用這個servlet以后要求控制臺打印出hello world。

package JSP;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class Hello
?*/
@WebServlet("/Hello")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public Hello() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("hello world");
}
}

4、創(chuàng)建一個servlet,調(diào)用這個servlet以后要求控制臺打印出今天是星期幾。

package JSP;
import java.io.IOException;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class Week
?*/
@WebServlet("/Week")
public class Week extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public Week() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request,?
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,?
HttpServletResponse response) throws?
ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request,
*? HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,?
HttpServletResponse response)
throws ServletException,
IOException {
String Array[]={"星期日","星期一","星期二",
"星期三","星期四","星期五","星期六"};//字符串?dāng)?shù)組
Calendar rightNow=Calendar.getInstance();
/**
? ?* 總結(jié):
* getInstance這個方法在單例模式用的甚多,為了避免對內(nèi)存造成浪費,
* 直到需要實例化該類的時候才將其實例化,所以用getInstance來獲取該對象,
* 至于其他時候,也就是為了簡便而已,為了不讓程序在實例化對象的時候,
* 不用每次都用new關(guān)鍵字,索性提供一個instance方法,不必一執(zhí)行這個類就
* 初始化,這樣做到不浪費系統(tǒng)資源!單例模式 可以防止 數(shù)據(jù)的沖突,節(jié)省內(nèi)存空間*/
int XiaBiao=rightNow.get(rightNow.DAY_OF_WEEK);//獲取時間
System.out.println("今天是"+Array[XiaBiao-1]);
//通過數(shù)組把周幾輸出
//------------------------------------------------------------
}}


5、創(chuàng)建一個servlet,調(diào)用這個servlet以后要求控制臺打印出一個九九乘法口訣表。
代碼不準(zhǔn)寫在同一個servlet類中,不準(zhǔn)使用注解

package JSP;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class NineNine
?*/
@WebServlet("/NineNine")
public class NineNine extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public NineNine() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request,
*? HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,?
HttpServletResponse response) throws?
ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request,
* \ HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,?
HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
for(int j=1;j<=9;j++){
for(int i=1;i<=j;i++){System.out.print
(j+"*"+i+"="+i*j+" ");}System.out.println
();
}
}
}



servlet小型應(yīng)用程序【[?s?rvl?t]】
例子:
手動創(chuàng)建一個servlet,運行它的時候在后臺打印Hello world.










或按10的步驟鼠標(biāo)右鍵等后運行代碼:

下面是運行的結(jié)果:

用快捷鍵或快捷方式選項等創(chuàng)建一個servlet:


或者點other后點Servlet:




快捷方式自動生成訪問路徑,web.xml中的就可以不用寫訪問路徑的部分:



——————


或








運行后:






————————
HTTP協(xié)議的get和post的請求方式:

Servlet簡介:

關(guān)于打開和編輯xml文件的方法:


關(guān)于HTTP協(xié)議的PPT:





POST 方法
用于發(fā)送敏感信息(如信用卡號)或要保存到數(shù)據(jù)庫中的信息
請求的頁面不能設(shè)置為書簽或不能通過電子郵件發(fā)送
發(fā)送的數(shù)據(jù)沒有大小限制











SERVLET開發(fā)入門PPT:













