Skip to main content

Posts

Showing posts with the label Servlet

Session management in servlet.

   In normal java web application the user name and other user related information are stored it into session and displayed into all the user pages. The best example is user name, because you need to display the user name into all the pages. It is achieved in following ways. Note the session management is very important in java application because the performance of the application based on the session management. So don't save unnecessary information into session. The following example gives one small idea about session management in java web application.   In this application I have created one java class for storing user related information. Because instead of using more keys for storing values into session, I have created the one java class and I have stored this class object into session (UserResults). This is good practice for storing values into session. 1. Create a UserResults class and include the following code. This class basically created for the purpose o...

Getting ServletContext init parameters that are defined in web.xml

In this example I am going to create some context parameter for performing database operation. The parameter names are   a. DBUserName   b. DBPassword   c. DatabaseUrl In servlet, I am going to get this details for performing database operation. 1. Create a new dynamic web project using eclipse. 2. Create a one new servlet and include the following code. package   com.controller; import   java.io.IOException; import   javax.servlet.ServletConfig; import   javax.servlet.ServletContext ; import   javax.servlet.ServletException; import   javax.servlet.annotation.WebServlet; import   javax.servlet.http.HttpServlet; import   javax.servlet.http.HttpServletRequest; import   javax.servlet.http.HttpServletResponse; @WebServlet ( "/LoginController" ) public   class   LoginController   extends   HttpServlet {         private   static   final ...

Getting servlet init parameter that is defined in web.xml

1. Create a new dynamic web project using eclipse. 2. Create a one new servlet and include the following code. package com.controller ; import java.io.IOException; import javax.servlet .ServletConfig; import javax.servlet .ServletException; import javax.servlet .annotation.WebServlet; import javax.servlet .http.HttpServlet; import javax.servlet .http.HttpServletRequest; import javax.servlet .http.HttpServletResponse; @ WebServlet ( "/LoginController" ) public class LoginController extends HttpServlet {                 private static final long serialVersionUID = 1L;                 public LoginController() {         super ();     }     protected void doPost( HttpServletRequest request, HttpServletResponse response)...

Creation of simple login application using Servlet, Testing the application using JUnit, and Deloying the application into Apache server.

Here I am using Eclipse for developing this application. In this application i hard coded user name and password. In real application these values are fetched from database. 1. Create a new dynamic web project and name it as ServletStudy. 2. Inside the WebContent folder ,create a index.jsp file and include the following code.       <%@ page language="java" contentType="text/html; charset=ISO-8859-1"           pageEncoding="ISO-8859-1"%>           <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org            /TR/html4/   loose.dtd">          <html>               <head>              ...