Danh mục tài liệu

Java Server Pages: A Code-Intensive Premium Reference- P9

Số trang: 10      Loại file: pdf      Dung lượng: 302.67 KB      Lượt xem: 9      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P9:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P9 Welcome to JSP You can see that the SubclassJSP provides empty jspInit() and jspDestroy() life-cycle methods,which satisfy the JSP subclass requirements. It also makes calls to its parents getUser() andgetCompany() methods.Now compile the PureJSPBase servlet to the /purejsp/WEB-INF/classes directoryand move the SubclassJSP.jsp to the /purejsp/ directory. You should then openyour browser tohttp://localhost:8080/purejsp/SubclassJSP.jsp?user=Bob&company=SamsYou should see a page similar to Figure 8.1.Figure 8.1: The output of SubclassJSP.jsp.SummaryIn this chapter, you saw how you can subclass JSPs to provide common utility methods. You also lookedat the requirements of both the superclass and the JSP subclass.In Chapter 9, we are going to cover using the JSPs implicit objects.Chapter 9: Using the JSPs Implicit ObjectsOverviewAs a JSP author, you have access to certain objects that are available for use in JSP documents withoutbeing declared first. These objects are parsed by the JSP engine and inserted into the generated servletas if you defined them yourself. - 81 -In reality the JSP engine recognizes the implicit object names and knows that they will be declared by, orpassed into, the generated servlet. Heres a example of a code snippet containing a _jspService()method:public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; String _value = null; try { if (_jspx_inited == false) { _jspx_init(); _jspx_inited = true; } _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType(text/html); pageContext = _jspxFactory.getPageContext(this, request, response, errorpage.jsp, true, 8192, true); application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); // begin out.write( Hello + JSP ); // end // begin [file=D:\hello.jsp;from=(7,6);to=(10,4)] // Print a simple message in the client area. out.println(Hello!); // end // begin out.write( ); - 82 - // end } catch (Exception ex) { if (out.getBufferSize() != 0) out.clear(); pageContext.handlePageException(ex); } finally { out.flush(); _jspxFactory.releasePageContext(pageContext); }}As we continue with the rest of this chapter, we will be examining exactly where, in the previous code,each of our implicit objects is declared. We will also look at examples, where applicable, of how you canuse each one of these objects. Note To run these examples, you will need to copy the JSP file from each of the following listings to the /purejsp/ directory.requestThe implicit object request represents the javax.servlet.http.HttpServletRequest object thatis passed into the generated _jspService() method. The HttpServletRequest interface defines anobject that provides access to HTTP-protocol–specific header information sent by the client. You can seehow it is passed in the following code snippet:public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {One of the more common uses for the request object is to access request parameters. You can do thisby calling the request objects getParameter() method, which is inherited from its parentjavax.servlet.ServletRequest, with the parameter name you are looking for. It will return a stringwith the value matching the named parameter. An example of this can be found in Listing 9.1.Listing 9.1: UseRequest.jsp UseRequest // Get the Users Name from the request out.println(Hello: + request.getParameter(user) + ); %> You can see that this JSP calls the request.getParameter() method passing in the parameter user.This looks for the key user in the parameter list and returns the value, if it is found. Enter the followingURL into your browser to see the results from this page:http://localhost:8080/purejsp/UseRequest.jsp?user=BobAfter loading this URL, you should see a screen similar to Figure 9.1.responseThe JSP implicit object response represents the javax.servlet.http.HttpServletResponseobject, which defines an object that provides the JSP with the capability to manipulate HTTP-protocol–specific header information and return data to the client. ...