The Servlet Session API refers to the documentation on the list of methods available in the HttpSession class that is used for Session Management
The HttpSession object is very powerful and has many features. Before we dig deep into the world of Sessions, let us take a look at the important methods they have so that we can use them effectively.
• getAttribute(java.lang.String name) - Returns an Object associated with that name that was stored in the session object.
• getAttributeNames() - Returns an Enumeration object that lists the names of the objects stored in the session object.
• getCreationTime() - Returns a long containing the date stamp of creation.
• getId() - Returns the id of the session as a String representing the unique identifier assigned to this session.
• getLastAccessedTime() - Returns a long containing the date stamp of the last time the session was accessed.
• getMaxInactiveInterval() - Returns an integer representing the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.
• invalidate() - This destroys a session. It can't be referenced after this method has been called.
• isNew() - This tells you whether the client knows about the session. In other words, the has session been created by the server, but the client hasn't acknowledged receiving the session ID yet.
• removeAttribute(java.lang.String name) - This removes an attribute. It deletes it from the session object.
• setAttribute(java.lang.String name, java.lang.Object value) - You use this method to add objects to a session.
• setMaxInactiveInterval(int interval) - Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
• getValue(java.lang.String name) - Returns an Object associated with that name that was stored in the session object. As of Version 2.2, this method is replaced by getAttribute(). The getAttribute() method will be on the exam, not this one, but it is here for completeness.
• getValueNames() - Returns a String array with a list of names associated with the objects added to a given session. As of Version 2.2, this method is replaced by getAttributeNames(). The getAttributeNames() method will be on the exam, not this one, but it is here for completeness.
• putValue(java.lang.String name, java.lang.Object value) - You use this method to add objects to a session. This has been deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object). The setAttribute() method will be on the exam, not this one, but it is here for completeness.
• removeValue(java.lang.String name) - This removes a value, but retains an attribute name in the session. The name is valid but the object is null. This has been deprecated. As of Version 2.2, this method is replaced by removeAttribute(). The removeAttribute() method will be on the exam, not this one, but it is here for completeness.
servlet is a small program which run on serverside,and it can not be downloaded at client side. java servlets dynamically extends the functionalities of a web server. main use is: session tracking: no of users (unique) visited u'r website. in order to use cookies..
A Servlet Mapping is a directive in the web.xml that tells the Servlet Container which class to use when a particular Servlet is called. A Servlet is a class within your Java Web Application. Let's say you have a servlet called MyServlet in the com.example.servlet package. You would need to have a Servlet Mapping pointing the path "/MyServlet" to the "com.example.servlet.MyServlet" class. Without the servlet mapping, you would not be able to invoke your servlet because the Servlet container would not know where it is. JSPs are different - they do not need mappings like this. JSPs exist within the WebRoot of the application, so they are always available. Servlets exist in the WEB-INF\Classes directory once your application is deployed.
Methods in the Servlet ContextApart from setting and getting custom attributes used for our application, the context also contains various methods that we can use to retrieve specific information about the application and other aspects. They are:• getAttributeNames() - Returns an Enumeration object containing the attribute names available within this servlet context.• getContext(String uripath) - Returns a ServletContext object that corresponds to a specified URL on the server.• getInitParameter(String name) - Returns a string containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.• getInitParameterNames() - Returns the names of the context's initialization parameters as an Enumeration of string objects, or an empty Enumeration if the context has no initialization parameters.• getMajorVersion() - Returns the major version as an int of the Java Servlet API that this Servlet Container supports.• getMimeType(java.lang.String file) - Returns the MIME type as a string of the specified file, or null if the MIME type is not known.• getMinorVersion() - Returns the minor version as an int of the Servlet API that this Servlet Container supports.• getNamedDispatcher(String name) Returns a RequestDispatcher object that acts as a wrapper for the named servlet.• getRealPath(String path) - Returns a string containing the real path for a given virtual path.• getRequestDispatcher(String path) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.• getResource(String path) - Returns a URL to the resource that is mapped to a specified path.• getResourceAsStream(String path) - Returns the resource located at the named path as an InputStream object.• getServerInfo() Returns the name and version as a String of the Servlet Container on which the servlet is running.
To retrieve data from a JSP to a servlet, you can use form elements in your JSP to collect user input and submit it to the servlet. When creating the form, ensure to set the action attribute to the servlet's URL and the method attribute to either GET or POST. In the servlet, you can access the submitted data using the request.getParameter("parameterName") method, where "parameterName" corresponds to the name attribute of the form input elements. This allows you to process the data as needed in your servlet.
To import javax.servlet in NetBeans 6.8, you need to ensure that your project is set up as a web application. Right-click on your project in the Projects pane, select "Properties," and then go to the "Libraries" category. Click on "Add Library," choose "Java EE" or "Servlet API" from the list (if available), or manually add the servlet JAR file from your local installation if it's not listed. After that, you can import the javax.servlet package in your Java classes.
Each web application contains a Session that can contain data that can be commonly made available to all the parts of the web application. The Session can be accessed from both the JSP and the Servlet. Ex: The servlet contains references to the HttpRequest object. So to access the session you can do so as below: HttpSession session = request.getSession();
it can be overcome by implementing session tracking & management in the running program on the server side. There are four solutions 1. Cookies, 2. Hidden from field, 3. URL rewriting and 4.Servlet Session API
The two scopes are: Request & Session
The Servlet 2.3 API consists of two packages: javax.servlet and javax.servlet.http. The base functionality is defined in the javax.servlet package whose classes and interfaces outline a generic, protocol-independent implementation. This means you can use it for non-Web applications, too. The javax.servlet.http interface defines classes and interfaces that are specific to the Hypertext Transfer Protocol (HTTP).
The Servlet 2.3 API consists of two packages: javax.servlet and javax.servlet.http. The base functionality is defined in the javax.servlet package whose classes and interfaces outline a generic, protocol-independent implementation. This means you can use it for non-Web applications, too. The javax.servlet.http interface defines classes and interfaces that are specific to the Hypertext Transfer Protocol (HTTP).
Servlets handle multiple simultaneous requests through a multi-threaded model provided by the Java Servlet API. When a servlet is deployed, the servlet container creates a separate thread for each incoming request, allowing multiple requests to be processed concurrently. This means that each request can be handled independently, with the servlet's service method being invoked in a separate thread for each request. However, developers must ensure thread safety when accessing shared resources to prevent data inconsistencies or conflicts.
Servlet Chaining means the output of one servlet act as a input to another servlet. Servlet Aliasing allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet alias. The output from first Servlet is sent as input to other Servlet and so on. The Output from the last Servlet is sent back to the browser. The entire process is called Servlet Chaining.
JavaServer Pages (JSP) are translated into servlets by the JSP engine during the compilation process. When a JSP file is requested for the first time, the server converts it into a servlet class, which involves converting JSP tags and expressions into Java code that adheres to the servlet API. This generated servlet is then compiled into bytecode and executed, allowing dynamic web content to be served. Subsequent requests utilize the compiled servlet, improving performance.
Hi its me Riaz Molwani : Session is a bessically class of hibernate API which provide the interface between the presistent data objects store and the application . .
Session in the JSP and Servlet context refers to an instance of the HttpSession object that contains all the information about the current user session with the web application. This can be used as a cache or temporary storage area to store values that might be required across the application.
servlet is a small program which run on serverside,and it can not be downloaded at client side. java servlets dynamically extends the functionalities of a web server. main use is: session tracking: no of users (unique) visited u'r website. in order to use cookies..
Http servlet and Generic servlet