Java?Web關(guān)鍵字填空示例詳解
(1)在TestServletRequest中將名為“param”,值為“HelloWorld”的信息存入request范圍內(nèi),并利用請(qǐng)求轉(zhuǎn)發(fā)方式轉(zhuǎn)向另一個(gè)Servlet——AnotherServlet。在AnotherServlet中,從request范圍內(nèi)讀取param的值。請(qǐng)將下面的Servlet程序補(bǔ)充完整。
文件名:TestServletRequest.java
@WebServlet("/TestServletRequest")
public class TestServletRequest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request._____1_____("param","HelloWorld");
RequestDispatcher rd=request.____2______("____3______");
rd._____4_____(request,response);
}
}
文件名:AnotherServlet.java
@WebServlet("/AnotherServlet")
public class AnotherServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String param= (String)request.____5______("param");
}
}
1. setAttribute
2. getRequestDispatcher
3. /AnotherServlet
4. forward
5. getAttribute
(2)在myweb應(yīng)用中,創(chuàng)建了Servlet名為HelloWorld,在其中讀取請(qǐng)求參數(shù)值并輸出,請(qǐng)將下面的Servlet程序補(bǔ)充完整。
public class HelloWorld extends ____1____ {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//對(duì)用戶提交的請(qǐng)求用utf-8來解碼,否則會(huì)出現(xiàn)亂碼
request.____2____("utf-8");
//通過設(shè)置Content-Type(內(nèi)容類型),告訴瀏覽器接下來發(fā)送的是HTML,編碼方式是UFT-8;
response.____3____("text/html;charset=UTF-8");
//獲取請(qǐng)求對(duì)象中name參數(shù)對(duì)應(yīng)的值
String name = request.____4_____("name");
String greeting = "Hello " + name + "!";
//獲取輸出流對(duì)象
PrintWriter out = response.____5___();
out.println(greeting);
}
}
1. HttpServlet
2. setCharacterEncoding
3. setContentType
4. getParameter
5. getWriter
(3)在myweb應(yīng)用中,在表單頁面輸入用戶名和密碼,并使用“post”方式將表單數(shù)據(jù)提交到Servlet程序,Servlet的mapping url配置為:/Process。在Servlet中判斷輸入的用戶是否為“admin”,如果是,則重定向到另一個(gè)Servlet(Admin.java),其mapping url為“/Admin”。
請(qǐng)將下面的Servlet程序補(bǔ)充完整。
_____11_____//配置Servlet的mapping url
public class Process extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取請(qǐng)求參數(shù)值
String un = request._____2_____(“username”);
if(un!=null ){
if(un._____3____(“admin”)){
response._____4_____(“____5_____”);
}
}
}
}
1. @WebServlet("/Precess")
2. getParameter
3. equals
4. sendRedirect
5. Admin
(4)在myweb應(yīng)用中,創(chuàng)建了一個(gè)Servlet名為Servlet1,其mapping url為:/Servlet1,在瀏覽器地址欄輸入地址訪問該Servlet,輸入地址如下:
http://localhost:8080/myweb/Servlet1?param1=111
在該Servlet中獲取param1參數(shù),并將其傳遞給另一個(gè)Servlet——Servlet2,并使用轉(zhuǎn)發(fā)請(qǐng)求方式跳轉(zhuǎn)到Servlet2。
請(qǐng)將下面的Servlet1程序補(bǔ)充完整。
public class Servlet1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取名為param1的請(qǐng)求參數(shù)對(duì)應(yīng)的值
String param1= request.____1______("___2____");
//保存到request范圍中
request._____3_____(“param1”,param1);
//獲取轉(zhuǎn)發(fā)器對(duì)象
RequestDispatcher rd=request._____4____("/Servlet2");
//使用轉(zhuǎn)發(fā)請(qǐng)求方式跳轉(zhuǎn)到Servlet2
rd.___5____(request,response);
}
}
1. getParament
2. paraml
3. setAttribute
4. getRequestDispatcher
5. forward
(5)過濾器
@WebFilter(
//僅對(duì)“/WebSite” URL格式進(jìn)行過濾
urlPatterns = { " ___1____" },
initParams = {
@WebInitParam(name = "site", value = "dalian")
})
public class MyFilter implements Filter {
protected String site;
public void destroy() {
}
public void ___2 ___ (ServletRequest req, ServletResponse res, FilterChain fchain) throws IOException, ServletException {
//如果site不為空,則輸出site
if(____ 3 _____) {
System.out.println(“網(wǎng)站”+site);
}
___4___.doFilter(req, res);
}
public void init(FilterConfig conf) throws ServletException {
//獲取@WebFilter注解中配置的初始化參數(shù)
this.site= ___5___.getInitParameter("site");
}
}
1. /Website
2. doFilter
3. site!=null
4. fchain
5. conf
(6) 在myweb應(yīng)用中,創(chuàng)建了Servlet名為Servlet1,Servlet1在會(huì)話范圍內(nèi)存放了一個(gè)名為“name”的屬性,然后使用重定向方式跳轉(zhuǎn)到Servlet2。在Servlet2中獲取會(huì)話范圍中的“name”屬性值并輸出。請(qǐng)將下面的Servlet2程序補(bǔ)充完整。
public class Servlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取當(dāng)前會(huì)話對(duì)象
HttpSession session=request._____1_____();
//獲取會(huì)話對(duì)象中的屬性值
String name= (String)session._____2_____("_____3_____");
//獲取輸出流對(duì)象
PrintWriter out=response._____4_____();
//輸出name
out._____5_____(name);
}
}
1. getSession;
2. getAttribute;
3. name;
4. getWriter;
5. print
總結(jié)
到此這篇關(guān)于Java Web關(guān)鍵字填空的文章就介紹到這了,更多相關(guān)Java Web關(guān)鍵字填空內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決OkHttp接收gzip壓縮數(shù)據(jù)返回亂碼問題
這篇文章主要介紹了解決OkHttp接收gzip壓縮數(shù)據(jù)返回亂碼問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心的方法
本文主要介紹了SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫的配置方法
這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫的配置方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03
Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書管理系統(tǒng)
這篇文章主要介紹了Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書館管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
MyBatis常用的jdbcType數(shù)據(jù)類型
這篇文章主要介紹了MyBatis常用的jdbcType數(shù)據(jù)類型的相關(guān)資料,需要的朋友可以參考下2016-12-12
JVM調(diào)優(yōu)參數(shù)的設(shè)置
Java虛擬機(jī)的調(diào)優(yōu)是一個(gè)復(fù)雜而關(guān)鍵的任務(wù),可以通過多種參數(shù)來實(shí)現(xiàn),本文就來介紹一下JVM調(diào)優(yōu)參數(shù)的設(shè)置,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Spring中容器的創(chuàng)建流程詳細(xì)解讀
這篇文章主要介紹了Spring中容器的創(chuàng)建流程詳細(xì)解讀,Spring?框架其本質(zhì)是作為一個(gè)容器,提供給應(yīng)用程序需要的對(duì)象,了解容器的誕生過程,有助于我們理解?Spring?框架,也便于我們“插手”這個(gè)過程,需要的朋友可以參考下2023-10-10

