Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 36 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
36
Dung lượng
590,5 KB
Nội dung
Chapter 11. Advancedservlet ITSS Java Programming Dr. NGUYEN Hong Quang, FIT-HUT HttpServletRequest interface Outline provide request information for HTTP servlets. The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc). Request information Client request information including : parameter name and values, and an input stream Attributes : custom information about a request HttpServletRequest Interface Operating with attributes Extends the ServletRequest interface Returns the value of attribute : public Object getAttribute(String name) Stores an attribute in this request : public void setAttribute(String name, Object o) HttpServletRequest Interface Forwarding : cooperation between several Servlets … Client Web browser Display reading page URL address Internet Application server Request Response Link in a Web page Servlet 1 Servlet 2 Servlet N • Require parameters • Proccessing data • Connecting to database • … Forwarding : methods public RequestDispatcher getRequestDispatcher (String path) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path public void forward (ServletRequest request, ServletResponse response) Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. Forwarding : usage example public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // proccessing data … // Get RequestDispatcher object RequestDispatcher rd = req.getRequestDispatcher(“/servlet2”); // forwarding rd.forward(req, res); } Application example inputname_forward.htm helloPersonForward 2 Servlet Display information Transmission of information helloPerson Forward2 Servlet Setting attributes + Name : input name + Date : access time (new information) Forward inputname_forward.htm <form method="post" action="helloPersonForward1"> <center> <h1> Test of forward servlets</h1> <hr><br> <p> Input the name.</p> <input type="text" name="name"> <input type="submit" value=" Transmission"> </center> </form> Application example Class HelloPersonForward1 (1) public class HelloPersonForward1 extends HttpServlet { // Taking out of input parameter String name = req.getParameter("name"); // Acquisition of access time Date d = new Date(); String date = d.toString(); // Name and the access time are stored in the HttpServletRequest object req.setAttribute("name", name); req.setAttribute("date", date); Application example [...]... interval) the servlet container will keep this session open between client accesses After this interval, the servlet container will invalidate the session Invalidates this session public void invalidate() Sample application Another session TestSession1 .java (1) public class TestSession1 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,... Web Browser Servlet Cookies Cookie Example Sent to server Server add information testCookie.htm Cookie Example Cookie on the client side document.cookie="clientCookie=clientCookieValue"; document.write(document.cookie); CookieListing .java (1) Cookie... action="cookieListing"> CookieListing .java (1) Cookie Example public class CookieListing extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter pw = res.getWriter(); pw.println("Cookie Listing"); pw.println("Cookie... i++) pw.println(cookies[i].getName() + " : " + cookies[i].getValue()); CookieListing .java (2) Cookie Example pw.println("Cookie received from server "); Cookie ck = new Cookie("serverCookie", "serverCookieValue"); res.addCookie(ck); pw.println(""); pw.println("document.write(document.cookie);"); pw.println(""); pw.println("");... req.getRequestDispatcher("/helloPersonForward2"); rd.forward(req , res); Application example Class HelloPersonForward2 (1) public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("doPost"); // Data is acquired from the HttpServletRequest object String name = (String)req.getAttribute("name"); String date = (String)req.getAttribute("date"); Application... getAttribute(String name) Return the Object of the attribute specified by name Return null if name doesn’t exist Usage example Session object manipulation public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { … session.setAttribute("countHit", new Integer(count)); … Integer i = (Integer)session.getAttribute("countHit"); if (i != null) { count = i.intValue()... i.intValue() + 1; } … } Deletion of information Syntax : public void removeAttribute(String name) Remove the attribute specified by name Usage example : public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { … HttpSession session = req.getSession(true); … session.removeAttribute(“countHit”); } Session Information (1) creation time : public long... HttpSession associated with this request or, if there is no current session and create is true, returns a new session Usage example Session object manipulation public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { … HttpSession session = req.getSession(true); … } Session object manipulation Manipulation of Information Information : Attributes Setting... Internet browser, each browser is managed by one session Session object manipulation Each session is created by servlet container and identified by a unique identification (id) Client Request Session Object Making Setting of Information Response + SessionID Request + SessionID Response + SessionID Servlet Instance Session Object: ID : xxxxxxx Information Searching session object Refer/Update of information... HttpSession session = req.getSession(true); TestSession1 .java (2) // declare “count” to count the number of page hit int count = 1; // take “countHit” attribute Integer i = (Integer)session.getAttribute("countHit"); if (i != null) count = i.intValue() + 1; // set “countHit” attribute session.setAttribute("countHit", new Integer(count)); TestSession1 .java (3) PrintWriter pw = res.getWriter(); pw.println(""); . Chapter 11. Advanced servlet ITSS Java Programming Dr. NGUYEN Hong Quang, FIT-HUT HttpServletRequest interface Outline provide request information for HTTP servlets. The servlet container. cookies"> </center> </form> Cookie Example CookieListing .java (1) public class CookieListing extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException. void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System. out.println("doPost"); // Data is acquired from the HttpServletRequest object String