Servlet Parameters - Jonathan Penava PDF

Title Servlet Parameters - Jonathan Penava
Course Enterprise Java Development
Institution Sheridan College
Pages 7
File Size 338.5 KB
File Type PDF
Total Downloads 58
Total Views 143

Summary

Jonathan Penava...


Description

Servlet Parameters Thus far we have made a servlet that generates an HTML page in Exercise 1 and in Homework Assignment 2. In both cases, we have mapped a URL to execute the servlet directly. But what if we want to execute the servlet with parameters that we provide (like parameters in a method). In a method definition, we provide a parameter to alter the method behaviour based on the values entered: public int add(int a, int b) { return a+b; }

Our servlet doGet method also has parameters, but they are fixed as part of the method definition. public void doGet(HttpServletRequest request, HttpServletResponse response){ //Some code goes here }

When Tomcat is given a request for a particular servlet, it first generates a Request and Response object, then sends it to the requested servlet.

Within the request object, we are able to provide parameters for the servlet. There are 3 types of parameters we are able to give to a servlet: 1. Request Parameters 2. Init-Parameters 3. Context Parameters

Request parameters are provided by the Query String of our URL. Go to your Homework Assignment 1 where you made the login page.

Your login page had 2 text fields, a name and a password. If you enter values into the name and password you would have gone to a screen that looks as follows:

While this is not what we want when someone actually runs our web application, it does tell us a lot about parameters and linking html pages to servlets.

Resource The first thing to note is the change in the URL resource.

Within the same resource path as our HTML page, the URL has changed looking for a resource called date2. There is obviously nothing in our Exercise1 folder called date2 because Tomcat returned a 404 error. The resource we link to could be a mapping to a servlet. If we created an XML file that mapped a servlet to the pattern /date then our server would attempt to execute that servlet and return what that servlet executed.

Query String

You should have also noticed that a query string has been added to our URL. The first text field was given the name param1, and the second text field was given the name param2. (This is done within the HTML code of the login page. You can name text fields whatever you like.) In this case I placed Jonathan in the name text field and secret in the password text field. When I clicked submit, the String values that I entered into both text areas were added as part of the query string.

HTML Code Taking a closer look at the HTML code for our login page will indicate where our resource and parameter names are defined.

Name: Password:



 Notice that there is no "/" in front of the resource. If you had to add an additional path to the current resource location, then the path would have to be added

Getting Query String Parameters In The Servlet Assuming that you make a servlet, and map it to the resource date2, how do you get the query string parameters? When Tomcat receives the URL request, it will take the query string and adds it to the request object that gets sent to the servlet. It is within the HttpRequest parameter that our values can be found. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ String s1 = request.getParameter("param1"); String s2 = request.getParameter("param2"); ... You must use the parameter names defined within the HTML file exactly in order to get the appropriate parameter. Once in your servlet you may manipulate their values as Java Strings.  A post request handles parameters the exact same way. The only difference is that the URL will not display the Query String.

Init Parameters and Context Parameters We can also define additional parameters that do not come from the HTML page, or from the client. Within the web.xml file, we can add 2 additional types of parameters: init-parameters and context parameters. Assume that your servlet required the administrator's name and email (for whatever reason). This would likely not be information the client would know. The xml file provides the ability for us to add additional parameters to our servlet.

Init Parameters Within the tags of our XML file, we define a subset of tags that will contain our parameters. One set of tags is required for each additional init parameter. We can then retrieve these parameters from the servlet like our query string parameters. web.xml

……

adminEmail [email protected]

Servlet String s = getServletConfig().getInitParameter(“adminEmail”);

When the Container Initializes the Servlet

1. The container reads the Deployment Descriptor for the servlet, including the servlet init parameters 2. The Container creates a new ServletConfig instance for the servlet 3. The container creates a name/value pair of Strings for each servlet init parameter. 4. Container gives the ServletConfig references to the name/value init parameters 5. Container creates a new instance of the servlet class 6. Container calls the servlet’s init() method, passing in the reference to the ServletConfig.

Context Parameters Init Parameters are specific to the servlet that they are defined in. If you created a larger application, and wanted to share information between multiple servlets (and JSP's which we will discuss later), then you would need a global parameter. A context parameter is defined outside of the servlet tags, and is accessible by all objects within the web application.

web.xml

adminEmail [email protected]

…….

Servlet String s = getServletContext().getInitParameter(“adminEmail”);

ServletConfig is one per servlet ServletContext is one per web application

Getting Things Straight    

Remember that it is one ServletConfig per servlet, and one ServletContext per web application. In the xml file the init parameters are defined in the servlet tags, while the context parameters are defined in the web-app tags. Both the init and the context use the method getInitParameter() Only when the web app is redeployed does the servlet and the rest of the web applications see the changes to the web.xml. If you change a web.xml file you need to restart the server. Init parameters are deploy-time constants.

All Parameters are Strings It is important to remember one thing regarding all 3 types of parameters, they are all of type String. This is an important distinction from Attributes which we will discuss later....


Similar Free PDFs