Java @ Desk: Servlets
Showing posts with label Servlets. Show all posts
Showing posts with label Servlets. Show all posts

Thursday, July 18, 2013

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

12:44 AM 1
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.

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.

Wednesday, July 3, 2013

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

12:36 AM 0
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.

Tuesday, May 21, 2013

How to configure ServletConfig in web.xml?

2:16 AM 0

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



Get init param of servlet using : getServletConfig().getInitParameter("email")

How to configure ServletContext in web.xml

2:01 AM 0
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


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

1:52 AM 0
The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to servlets.


Explain pre initialization of servlet

12:09 AM 0
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.

Monday, May 20, 2013

Difference between GET and POST

11:57 PM 0
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

5:21 AM 0
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