Java @ Desk: Servlets | Java at your desk

Difference between sendRedirect() and forward() in jsp servlet Java?

sendRedirect() and forward() methods are applicable to both servlets and jsp in java. Since jsp converts to a servlet itself, these methods can be used inside a scriplet in a jsp.

sendRedirect():
This method is called from HttpServletResponse object. This method from response object expects an URL where the response is required to redirect. This sends the user to different location located on a different server altogether. This happens on a client side or browser. The new address will be visible in the browser.

Redirect sets the response status to 302, and the new url in a Location header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url.

How to get login attributes from a servlet/jsp or in web service?

There are scenarios where we may require the user name and password of the system from where the servlet request is coming.

What is the need to get the login attributes?
1) The very basic requirement to do that is to authenticate the user from where the request is coming. We may require to authenticate the user who is requesting for a servlet for security purposes.
2) To check for access rights. Say for example, we may block some user from accessing the servlet due to some violation. We can do the same in a servlet if we have the user information.

How to configure ServletConfig in web.xml?







ServletConfig is configured in web.xml and parameters in this mapping is accessible to this servlet only





<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>com.MyClass</servlet-class>

<init-param>
<param-name><b>email</b></param-name>
<param-value>admin@email.com</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/Demo</url-pattern>
</servlet-mapping>
Get init param of servlet using : getServletConfig().getInitParameter("email")

How to configure ServletContext in web.xml

ServletContext can be configured in web.xml and it will be accessible to all servlets in the web application. It is specified as mentioned below

<context-param>
<param-name>adminEmailAddress</param-name>
<param-value>admin@admin.com</param-value>
</context-param>

The value of every specified parameter can be loaded in the servlet code, referenced by the parameter name:
String value = getServletContext().getInitParameter("adminEmailAddress");

Servlet mapping in web.xml

The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to servlets.

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

Explain pre initialization of servlet

Pre initialization of servlet is something the servlet gets loaded on server startup.

Generally, the servlet gets initialized when it receives first client request. This is called lazy loading of servlet.

When for a servlet <load-on-startup> in web.xml is defined as 1, then that servlet will gets initialized on server startup. This is called pre initialization of servlet.

Difference between GET and POST

GET POST
Data is passed through header. Hence there is a limitation in the amount of data to send. Data is passed in body. Hence large amount of data can be passed.
Get method is not secured, since data is visible in header It is secured because data is not exposed.
Can be bookmarked Cannot be bookmarked
It is idempotent It is non-idempotent

Explain lifecycle of servlet in java

1) init() – Servlet enters into this phase whenever first request for the servlet is requested by the client. Servlet gets initialized based on value in web.xml. If the value is 0 then it will be initialized on first client request otherwise it will be initialized on server startup

public void init(ServletConfig config) throws ServletException