We can define filters in JSP to intercept requests from a client or to change response from a server.
Filter is a Java class that is defined in the deployment descriptor of web.xml of an application. The JSP container reads filter from web.xml and applies a filter as per the URL pattern associated with the filter.
JSP Engine loads all the filters in when we start the server.
To upload a file by JSP we can use <input type=”file”> in the Form data being passed from HTML.
If the file is very large in size, we can set enctype=multipart/form-data.
We have to use POST method in the Form to send a file.
Once the request is received, we can implement the logic to read mulitpart data in doPost() method of JSP. There are methods in JSP framework to read large files via this method.
To initialize multiple JSP objects, we have to specify same Servlet object multiple times in web.xml.
This indicates to JSP container to initialize separate JSP/Servlet object for each element. Each of the Servlet instance will have its own ServletConfig object and parameters.
Both forward and sendRedirect are mechanisms of sending a client to another page. The main difference between these two are as follows:
<li>In forward, the processing takes place at server side. In case of sendRedirect() the processing takes place the client side.</li>
<li>In forward, the request is transferred to another resource within same server. In case of sendRedirect the request can be transferred to resource on some other server.</li>
<li>In forward only one request call is consumed. In case of sendRedirect two request response calls are created and consumed.</li>
<li>The forward is declared in RequestDispatcher interface. Where as sendRedirect is declared in HttpServletResponse object.</li>
In JSP, config object is of type ServletConfig. This object is created by Servlet Container for each JSP page. It is used for setting initialization parameters for a specific JSP page.
We can specify both init-param and context-param in web.xml file.
We use init-param to specify the parameters that are specific to a servlet or jsp. This information is confined to the scope of that JSP.
We use context-param to specify the parameters for overall application scope. This information does not change easily. It can be used by all the JSP/Servlet in that Container.
We use RequestDispatcher interface to forward requests to other resources like HTML, JSP etc.
It can also be used to include the content of another page in a JSP.
It has two methods: forward and include.
We have to first get the RequestDispatcher object from the container and then we can call include or forward method on this object.
There is a built-in request object in a JSP that provides methods to read Form data. Some of the methods are as follows::
- getParameterNames(): This method returns the list of all the parameters in the Form.
- getParameter(): We call this method to get the value of parameter set in the Form. It returns null if the parameter is not found.
- getParameterValues(): If a Parameter is mentioned multiple times in a Form, we use request.getParameterValues() method to get all the values. This method returns an array of String values.
- getParameterMap(): This method returns the map of all the Parameters in Form.
We can use set the header in response object for Cache-Control to specify no caching.
Sample code is as follows:
We can use sendRedirect() method in JSP to redirect the request to another location or page.
In this case the request will not come back to server. It will redirect in the browser itself.
Sample code is as follows:
<% response.sendRedirect(URL); %>