You’ll now see how to access HTML form data using a Java servlet. If you recall from Table, there are a number of methods in the HttpServletRequest object that are specifically relevant to accessing HTML form data. Listing 1.4 is a basic HTML form.
The HTML for the form uses different kinds of input field and looks similar to Figure when displayed in a Web browser
To extract the data from this form and present it within a dynamically generated Web page, the servlet in Listing 1.5 could be used
The servlet in Listing 1.5 will process the data from the HTML form and write it out to a new Web page. Note the following features of this servlet:
The output from the servlet, in response to a completed form, would be as shown in Figure
The servlet would also need to be defined within the web.xml deployment descriptor.A modified version is shown in Listing
Because this file must adhere to a specific XML Schema, the
Earlier, you were introduced to the init(ServletConfig config) method that is used to initialize the servlet. In this section, attention will be given to this process, and how you can pass parameter values into your servlet.Those of you familiar with applet programming or even using Java applets on Web pages will have seen the parameters that can be passed to a Java applet using <param> tags in the HTML.With servlets, parameter values can also be passed from web.xml to servlets that can be used often at initialization.
The javax.servlet.ServletConfig object is key to this functionality. It contains the following two methods that enable you to access these parameters:String getInitParameter(String name) String getInitParameterNames()
Listing shows a web.xml file that contains parameters that are being passed into a Java servlet. Listing then shows a servlet that is accessing these parameter values.
Listing 1.7 web.xml Setting Servlet Initialization Parameters.
Listing 1.8 ParameterServlet.java —Extracting Initialization Parameters from web.xml
ParameterServlet.java accesses the parameters set in web.xml from within the initialization method.These are then presented on a Web page to the user.The two parameters shown are a username and password.These parameters could be used for a database connection, for example. Notice also that the call to super.init(config) is present.This must always be there as the first line in the init function.
The ServletContext is a very useful object, because it provides for a handle for the developer into the container.This is very important because it provides access to the following:
The main methods are shown in Table
Application-Wide Attributes and Parameters
You have already seen how parameters can be passed to individual servlets through web.xml.You can also pass in parameters that are visible to all servlets within a Web application. So for example, in Listing the parameters obtained are only available to that servlet.They might be relevant to a number of servlets, in which case they can be set as application parameters in the web.xml file. Listing shows a web.xml file that is now setting application-wide parameters that are accessible throughout the Web application via the ServletContext object.
Listing 1.9 web.xml Setting Application-Wide Parameters
Listing shows a modified version of the init() method from the servlet shown in Listing Instead of accessing servlet-specific parameters, the init() method shown here is accessing the application-wide parameters via the ServletContext.
Listing 1.10 Modified init() Method of ParameterServlet.java
Application-scoped attributes are different from parameters.Within any Web application there will be objects that ideally should be shared between all the parts of the Web application. Database connections and objects containing data that is required by all the servlets, such as product information on a commerce site, would be good examples. References to these objects can be stored in the ServletContext so that they can be referenced from any other part of the Web application. The following lines of code can be used to add a new object of type ProductList to the ServletContext:
Then, from some other servlet, the object could be accessed in the following way:
Logging with the ServletContext
Web applications are up and running for long periods of time, and it is vital to gather relevant and helpful information. Logging is a part of the servlet specifications, so all Web containers must implement some kind of logging functionality.Tomcat places log entries into log files located in the following location:
The log file that gets written to has the name:
So, an example on my machine would be
Listing demonstrates a servlet that writes to this log file.
Listing 1.11 LoggerServlet.java
Listing puts the following entry in the log file:2002-04-24 11:14:50 LoggingServlet: init 2002-04-24 11:14:50 Init has been invoked 2002-04-24 11:14:50 doGet has now been invoked 2002-04-24 11:18:26 doGet has now been invoked 2002-04-24 11:18:26 doGet has now been invoked
This log entry was generated by visiting a URL that invoked the servlet three times.
Forwarding Requests to Other Servlets
In Table, there are some additional methods associated with URLs and forwarding requests.The java.servlet.RequestDispatcher is an object that is made available to you via the ServletContext, and it provides you with the means to forwarding requests on to other servlets or JSPs.This is particularly important in sites that are using an architecture often referred to as the model 2 architecture.
The key methods of the RequestDispatcher arevoid forward(ServletRequest request, ServletResponse response) void include(ServletRequest request, ServletResponse response)
The forward method is used when a request is going to be serviced by a resource other than the servlet.The include method includes the output from another resource in the eventual output to the client. Figure demonstrates what happens to the request when the forward method is used.
The important thing to note is that this is not a redirection.The client only makes one request to the server.The fact that the response came from another servlet is transparent to the client.Listing shows a servlet that, when deployed, will forward the request to the basic servlet shown in Listing 1.2.
If you set up this servlet within your Web application by providing a <servlet> and <servlet-mapping> entry in web.xml, you will find that when you invoke it, the URL in your browser does not change.The response to the browser is simply returned from the basic servlet in Listing
The include method, on the other hand, simply includes the output from the specified resource. So, if in Listing you used the include method, the output from /myServlet would be included in any output from the ForwardingServlet. In other words, it is a programmatic server-side include.In JSP, as you’ll see later, this isachieved using tags.
JSP Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||Java Servlets Interview Questions|
|Hibernate Interview Questions||JavaServer Faces (JSF) Interview Questions|
|JSTL(JSP Standard Tag Library) Interview Questions||JBOSS Interview Questions|
|Log4j Interview Questions||NHibernate Interview Questions|
|Apache Struts 2 Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.