执行后的结果是: 你好 <%@include%> 静态引用则是将b.jsp所生成的servlet的源码合并到a.jsp的源码中,然后运行合并后的servlet文件输出结果。 在日常的项目应用中,我建议使用
6. 请列举至少三种EL表达式的内置对象,并说明其作用? requestScope 从request中取值 sessionScope 从session中取值 applicationScope ?你懂得 pageScope param paramValues
7. 列举至少三种JSTL核心库标签,并说明其作用?
a.jsp
b.jsp 你好
8. 请简述HttpServletRequest 、 HttpSession 、ServletContext 三个对象何时被创建、销毁? HttpServletRequest 从浏览器发起一个新的请求时,request对象被创建,当服务器返回response响应的时候请求被销毁。 HttpSession,某个浏览器窗口第一次发送请求的时候session就会被创建,sessionId, session在过期后被销毁(tomcat默认30分钟)或者在程序中调用sesssion.invalidate()销毁当前会话,再或者web应用关闭或重启的时候。 servletContext即application对象,在web应用启动的时候被创建,在web应用关闭或重启的时候被销毁。
9. 简述 request.getRequestDispatcher(“...”).forward() 方法与 response.sendRedirect()方法的区别?
Request.getRequestDispatcher.forward()是一次请求下的跳转,我们可以在b.jsp中使用request.getAttribute(“abc”)获取当前请求中的属性值。 在URL中http://localhost:8080/test/userservlet
a.jsp发送请求 doPost()处理来自于a.jsp的请求request.getRequestDispatcher().forward()UserServletb.jsp
sendRedirect它会发起两次请求,也就是意味着b.jsp永远无法获取到第一次请求的内容 在URL中http://localhost:8080/test/b.jsp
a.jsp提交请求处理请求response.sendRedirect(b.jsp)UserServletb.jsp跳转到
10. 什么是MVC,MVC的优点?
MVC : MVC的根本目的是让软件架构层面上进行“解耦”
Model:模型层:包含两部分只能:1业务逻辑的运算职能 2. 数据的持久化 View:视图层(表示层),接收用户输入、展现数据的结果
Controller : 控制层,它作为view和model的枢纽存在,它的作用是承上启下: Struts2中使用Action所谓控制层,Action的具体职能是:
1. 接收来自于客户端的数据
2. 调用Model层(Service类或者DAO类)将数据进行处理 3. 跳转界面,显示处理结果。
三.程序题 20分
完整书写com.handson.servlet.HelloServlet类代码,要求用户通过Get方式访问该Servlet对
象后,HelloServlet向浏览器输出“中国,您好!”这句中文,并将这句话保存在session的“title”属性中以备日后使用。 Package com.handson.servlet;
Public class HelloServlet extends HttpServlet{
Public void doGet(HttpServletRequest request , HttpServletResponse response) throws IOException{ String msg = “中国,你好!”; HttpSession session = request.getSession(); Session.setAttribute(“title” , msg); Response.setContentType(“text/html;charset=utf-8”); Response.setCharacterEncoding(“UTF-8”); PrintWriter out = Response.getWrite(); out.println(msg); out.flush();
}
Public void doPost(HttpServletRequest request , HttpServletResponse response) throws IOException{
This.doGet(request , response); }