(1)在TestServletRequest中將名為“param”,值為“HelloWorld”的訊息存入request範圍內,並利用請求轉發方式轉向另一個Servlet——AnotherServlet。在AnotherServlet中,從request範圍內讀取param的值。請將下面的Servlet程式補充完整。
文件名: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. forward5. getAttribute#(2)在myweb應用程式中,建立了Servlet名為HelloWorld,在其中讀取請求參數值並輸出,請將下面的Servlet程式補充完整。public class HelloWorld extends ____1____ { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //对用户提交的请求用utf-8来解码,否则会出现乱码 request.____2____("utf-8"); //通过设置Content-Type(内容类型),告诉浏览器接下来发送的是HTML,编码方式是UFT-8; response.____3____("text/html;charset=UTF-8"); //获取请求对象中name参数对应的值 String name = request.____4_____("name"); String greeting = "Hello " + name + "!"; //获取输出流对象 PrintWriter out = response.____5___(); out.println(greeting); } }1. HttpServlet2. setCharacterEncoding3. setContentType4. getParameter#5. getWriter
#4. getParameter
#5. getWriter
#(3)在myweb應用程式中,在表單頁面輸入使用者名稱和密碼,並使用「post」方式將表單資料提交到Servlet程序,Servlet的mapping url配置為:/Process。在Servlet中判斷輸入的使用者是否為“admin”,如果是,則重定向到另一個Servlet(Admin.java),其mapping url為“/Admin”。
請將下面的Servlet程式補充完整。
_____11_____//配置Servlet的mapping url public class Process extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求参数值 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應用程式中,建立了一個Servlet名稱為Servlet1,其mapping url為:/Servlet1,在瀏覽器網址列輸入位址存取該Servlet,輸入位址如下:
http://localhost:8080/myweb/Servlet1?param1=111
在該Servlet中取得param1參數,並將其傳遞給另一個Servlet——Servlet2,並使用轉送請求方式跳到Servlet2。
請將下面的Servlet1程式補充完整。
public class Servlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取名为param1的请求参数对应的值 String param1= request.____1______("___2____"); //保存到request范围中 request._____3_____(“param1”,param1); //获取转发器对象 RequestDispatcher rd=request._____4____("/Servlet2"); //使用转发请求方式跳转到Servlet2 rd.___5____(request,response); } }
1. getParament
2. paraml
#3. setAttribute
4. getRequestDispatcher
5. forward
#(5)過濾器
@WebFilter( //仅对“/WebSite” URL格式进行过滤 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(“网站”+site); } ___4___.doFilter(req, res); } public void init(FilterConfig conf) throws ServletException { //获取@WebFilter注解中配置的初始化参数 this.site= ___5___.getInitParameter("site"); } }
1. /Website
2.doFilter
3.site!=null
4. fchain
5. conf
(6) 在myweb應用程式中,建立了Servlet名稱為Servlet1,Servlet1在會話範圍內存放了一個名為「name」的屬性,然後使用重定向方式跳轉到Servlet2。在Servlet2中取得會話範圍中的「name」屬性值並輸出。請將下面的Servlet2程式補充完整。
public class Servlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取当前会话对象 HttpSession session=request._____1_____(); //获取会话对象中的属性值 String name= (String)session._____2_____("_____3_____"); //获取输出流对象 PrintWriter out=response._____4_____(); //输出name out._____5_____(name); } }
1. getSession;
2. getAttribute;
3. name;
4. getWriter;
###5. print ###以上是Java Web關鍵字的填空範例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!