Java Web关键字填空示例详解
(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. forward
5. 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. HttpServlet
2. setCharacterEncoding
3. setContentType
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关键字填空的文章就介绍到这了,更多相关Java Web关键字填空内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!