Monday, 5 November 2012

J2ee Tutorial Part 1 -> Sample web application

//creating a sample web application using eclipse and tomcat server

Step 1 :-Download Eclipse  and Apache tomcat 7.0
Step 2 :-Run Eclipse and add Tomcat as a server to Eclipse
Step 3 :-Go to file new and create a new dynamic web project


Step 4:- Go to web.xml in the WEB-INF project and change it as follow

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>rakesh</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file> //this jsp page will be loaded first
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>loginServlet</display-name>
    <servlet-name>loginServlet</servlet-name>//this will be servlet name
    <servlet-class>com.tcs.practice.servlets.loginServlet</servlet-class>//this is our servlet
  </servlet>
  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>//url mapping so that using this we can invoke the servlet
  </servlet-mapping>
</web-app>

Step 5:- Create a jsp with name login under the webcontent folder this will be the first page to be loaded

paste the bellow content in the jsp

<%@ 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>
<h1 align="center">login</h1>
<body>
<form action="<%=request.getContextPath()%>/loginServlet" method="post">
<fieldset>
User name: <input type="text" name="UserName" /><br />
<br />
Password : <input type="password" name="Password" /><br><br>
<input type="submit" value="Submit" />
<a href="reg.jsp">register</a>
</fieldset>
</form>
</body>
</html>

               the above is a jsp which will ask for login information if the login details are correct i will go to success.jsp if the details are incorrect it will go to error .jsp

Step 6:- Create two jsps with one Success.jsp with bellow content
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>welcome to home page</h1>
</body>
</html>
Step 7:- Create  error .jsp with bellow content
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>error</h1>
</body>
</html>

Step 8:- Go to src folder and create a new package com.tcs.practice.servlets and right click on the package and create a new servlet with name LoginServlet and paste the bellow content bellow


package com.tcs.practice.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tcs.practice.dao.CheckLogin;
import com.tcs.practice.dto.LoginBean;

/**
 * Servlet implementation class loginServlet
 */
public class loginServlet extends HttpServlet {//for a class to be a servlet it should extend generic servlet
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
      
        String username=request.getParameter("UserName");//getting username from request
      
        System.out.println("this is username"+username);
        String password=request.getParameter("Password");//getting password from request
        System.out.println("this is password"+password);
        
        if(username.equals("admin")&&passsword.equals("admin"))
        response.sendRedirect("success.jsp"); //if matches the success.jsp will be loaded
        else
        response.sendRedirect("error.jsp");//if error the error .jsp will be loaded
          
    }

}

Step 9:-Rightclick on project clean build the project and then run the project
Step 10:-End of tutorial part one.

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...