How to run jsp program in eclipse using tomcat server

  • Hi friends today we will see basic example of how to run jsp (java server pages) program using tomcat server in eclipse IDE.
  • For this we need to create dynamic web project in eclipse
  • Lets see how to run jsp program in tomcat server step by step.
  • Print hello world using jsp program


Required software: 



  • After downloading eclipse and tomcat server. open eclipse and add server and select tomcat version and giver downloaded tomcat folder path.

Step 1: Open eclipse and create dynamic web project:

  • Now we need to create dynamic web project for this in eclipse 
  • New => other=> web=> Dynamic web project


how to run jsp program in tomcat server eclipse



Step 2 : Create a dynamic web project

  • Click on next => and give MyFirstJsp as project name.
  • Click on Next=> click next here select generate xml deployment descriptor check box
  • And click on finish.
  • So now project will be created and with default xml file.


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javae
  4. /web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  6.   <display-name>MyFirstJsp</display-name>
  7.   <welcome-file-list>
  8.     <welcome-file>index.html</welcome-file>
  9.     <welcome-file>index.htm</welcome-file>
  10.     <welcome-file>index.jsp</welcome-file>
  11.     <welcome-file>default.html</welcome-file>
  12.     <welcome-file>default.htm</welcome-file>
  13.     <welcome-file>default.jsp</welcome-file>
  14.   </welcome-file-list>
  15. </web-app>

Step 3: Create a JSP page


run jsp on eclipse


  • Right click on web content folder and select new => JSP file => give index.jsp


Step 4 : Edit Jsp page

  • Edit the JSP page and give Page title and in the body section create one paragraph tag and write some text like Hello world.


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Welcome</title></head>
  9. <body>
  10. <p>Hello World</p>
  11. </body>
  12. </html>


Step 5: Run dynamic web project using tomcat server

  • So index.jsp is present in welcome file list of web.xml file then whenever server starts it loads welcome file i.e index.jsp
  • We can give any name t our jsp but it should present in web.xml as welcome file
  • <welcome-file>index.htm</welcome-file> . no need of other files which names are created by default we can delete those things.
  • Right click on the project and => run as => run on server=> select tomcat version = >add our project if not added click on finish. 
how to compile jsp program in tomcat

Java Jsp Interview Questions

1.Explain JSP life Cycle methods?

  • jsp engine controls the life cycle of jsp
  • Jsp life cycle is described by three life cycle methods and six life cycle phases


Jsp life cycle methods :

  • jspInit()
  • _jspService()
  • jspDestroy()


  • Jspinit() method is invoked when JSP page is initialized.
  • The _jspService() method corresponds to the body of the jsp page.
  • This method is defined automatically by the JSp container and should never be defined y the JSP page author.

Jsp Life cycle Phases: 

  • Translation phase
  • Compilation phase
  • Instantiation phase
  • initialization phase
  • Servicing phase
  • Destruction phase

Read More at : JSP Life Cycle

2. What are the Jsp Implicit Objects?


  • Implicit objects in jsp are the objects that are created by the container automatically and the container makes all these 9 implicit objects to the developer.
  • We do not need to create them explicitly, since these objects are created and automatically by container and are accessed using standard variables(objects) are called implicit objects.
  • The following are of implicit variables available in the JSP.  


  1. out
  2. request
  3. response
  4. config
  5. application
  6. session
  7. pagecontext
  8. page
  9. exception
Read More at: 9 Jsp Impicit Objects in java

3. Explain Jsp Scripting Elements?

  • Jsp scripting elements are used to embed jva code in to the jsp.
  • Jsp scripting elements are three types
    1. Declaration
    2. Expression
    3. Scriplet
Read More at: Jsp Scripting Elements



4. What are the Directives in Jsp?

  • A jsp directive is a translation time instruction to the jsp engine.
  • we have three kinds of directives.
  1. Page directive
  2. Include directive
  3. Taglib directive

 Read More at: Jsp Directive Elements


 5. What are the differences between include directive and include action?


Java Jsp interview Questions

 

6. How to disable session in JSP?

  • By using page directive
  • <%@ page session="false" %> 

7. How can we handle exceptions in a JSP page?

  • We can handle exception in jsp by using error page element of page directive 
  • The errorPage attribute tells the JSP engine which page to display if there is an error while the current page runs.
    The value of the errorPage attribute is a relative URL.
    The following directive displays MyErrorPage.jsp when all uncaught exceptions are thrown

    Syntax:

    errorPage="url"

    Example:

    <%@ page errorPage="error.jsp"%>

8.How can we override jspInit() and jspDestroy() methods in a jsp?

  • Both methods are executed once in a life cycle of a jsp.
  • We can override these two method as shown below.
  • <%!
        public void jspInit() {
            . . .
        }
    %>
  • <%!    
        public void jspDestroy() {
            . . .   
        }
    %>

9.Can a JSP extend a java class?

  • Yes we can extend a java class in jsp
  • <%@ include page extends="classname" %>
  • We can do this because jsp will convert to a servlet so a servlet can extend a class its fine

10.How can we pass information from one jsp to included jsp page?

  • <jsp:include page="employeedetail.jsp" flush="true">
  • <jsp:param name="name" value="abc"/>
  • <jsp:param name="id" value="220"/>

JSP Implicit Objects

  • Implicit objects in jsp are the objects that are created by the container automatically and the container makes all these 9 implicit objects to the developer.
  • We do not need to create them explicitly, since these objects are created and automatically by container and are accessed using standard variables(objects) are called implicit objects.
  • The following are of implicit variables available in the JSP.  

9 Jsp implicit Objects

1.out
2.request
3.response
4.config
5.application
6.session
7.pagecontext
8.page
9.exception



1.out:

  •  out is one the implicit object to write the data to the buffer and send output to the client in response.
  • out object allows us to access the servlets output stream and has a pagescope.
  • out.print("");
  • out is the object of javax.servlet.jsp.JspWriter class.
  • While working with servlet we need printWriter object
  • PrintWriter out=response.getWriter();   
  • In servlet we are used PrintWriter to send the output to the client. In JSP we use JSPWriter. 

Difference between JspWriter and PrintWriter:

  • Every JSPWriter is associated with 8KB of internal Buffer. Where as PrintWriter doesn’t associated with any Buffer.
  • The methods of the JspWriter class are designed to throw java.io.IOException where as the methods of PrintWriter class are not throw Exception.
  • JspWriter is an abstract class present in javax.servlet package.
  • PrintWriter is a class defined in java.io package.
  1. public class PrintWriter extends Writer { 
  2. // .....
  3. }
  4.  
  5. public abstract class JspWriter extends Writer {
  6. //....
  7. }

JspWriter out object in jsp:

  1. <html> 
  2. <body> 
  3. <%
  4.     int a = 10;
  5.     int b = 20;
  6.     out.print(“A value is:”+a);
  7.     out.write(“B value is:”+b);
  8. %>
  9. </body> 
  10. </html> 



Printwriter object in servlet:

  1. PrintWriter out=response.getWriter();  

2.request:

  • request implicit object is the object of  type  HttpServletRequest.
  • request object will be created by the container for every request.
  • It will be used to get request information like parameter , header information ,server name server port etc.
  • By using request object we can able to set ,get and remove attributes from request scope.
  • It uses the getPareameter() method to access request parameter.
  • The container passes this object to the _jspService() method.


  1. <form action="welcome.jsp">  
  2. <input type="text" name="username">  
  3. <input type="submit" value="submit"><br/>  
  4. </form> 

  1. <%   
  2. String name=request.getParameter("userid");  
  3. out.print("welcome "+name);  
  4. %> 

  1. out.println(request.getMethod()+”<br>”);
  2. out.println(request.getRequestURI()+”<br>”);
  3. out.println(request.getProtocol()+”<br>”);
  4. out.println(request.getHeader(“User Agent”)+”<br>”);
  5. out.println(request.getHeader(“Accept-language”)+”<br>”);

3.response:

  • response is an instance of class which implements the HttpServletResponse interface.
  • Container generates this object and passes to the _jspService() method as parameter.
  • response object will be created by the container for each request.
  • It represents the response that to be given to the client. 
  • The response implicit object is  used to content type, add cookie and redirect the response to another resource.

1.set content type.

  1. response.setContextType(“text/xml”);
  2. out.println(“<employee>”);
  3. out.println(“<eno>|</eno>”);
  4. out.println(“</employee>”);

2.Redirect response:

  1. <%  
  2.     response.sendRedirect("http://www.instanceofjava.com"); 
  3. %>

3. To send the error message directly to the client:

  1. <%  response.sendError(537,”xyz”); %>

4.config

  • config is one of the implicit object of type javax.servlet.ServletConfig.
  • config object is created by the web container for each jsp page.
  • config object used to get initialization parameters from web.xml file.


  1. <web-app>
  2.  
  3. <servlet>  
  4. <servlet-name>instanceofjava</servlet-name>  
  5. <jsp-file>/welcome.jsp</jsp-file>  
  6. <init-param>  
  7. <param-name>dname</param-name>  
  8. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  9. </init-param>  
  10. </servlet>  
  11.  
  12. <servlet-mapping>  
  13. <servlet-name>instanceofjava</servlet-name>  
  14. <url-pattern>/welcome</url-pattern>  
  15. </servlet-mapping>  
  16.  
  17. </web-app> 

  1. <%   
  2. String driver=config.getInitParameter("dname");  
  3. out.print("driver name="+driver);  
  4. %> 

  • servletConfig is an object used by servlet container to pass the information during initialization.
  • Once if we get the ServletConfig object, we can find the name of the servlet which is configured in web.xml file by using a method getServletName() method.

  1. <%
  2. String name = config.getServletName();
  3. oput.println(name);
  4. %>


5.application:

  • application is one of the implicit object of type javax.servlet.ServletContext.
  • application object is created by the web container one per application when the application gets deployed.
  • application object used to get context parameters from web.xml file.
  • ServletContext object contains set of methods which are used to interact with ServletContainer. By using these methods we can find the information about ServletContainer.
  • When ever sun micro system releases a new version of API. It is the responsibility of server vender to provide the implementation to the latest API. 
  • For example when servlet 2.5 is released Tomcat guys has provided the implementation and integrated it in Tomcat 6.0 the latest version of servlet API is 3.0 this version is integrated in Tomcat 7.0.
     

  1. <web-app>
  2.  
  3. <servlet>  
  4. <servlet-name>instanceofjava</servlet-name>  
  5. <jsp-file>/index.jsp</servlet-class>  
  6. </servlet>  
  7.  
  8. <servlet-mapping>  
  9. <servlet-name>instanceofjava</servlet-name>  
  10. <url-pattern>/index</url-pattern>  
  11. </servlet-mapping> 
  12.  
  13. <context-param>  
  14. <param-name>dname</param-name>  
  15. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  16. </context-param>  
  17.  
  18. </web-app>

  1. <%   
  2. String driver=application.getInitParameter("dname");  
  3. out.print("driver name="+driver);  
  4. %> 

6.session

  • session implicit object is holding HttpSession object.
  • session object used to set, get, and remove attributes to the session scope and also used to get session information.

index.html

  1. <html>  
  2. <body>  
  3. <form action="/welcome">  
  4. <input type="text" name="uname">  
  5. <input type="submit" value="submit"><br/>  
  6. </form>  
  7. </body>  
  8. </html>   

welcome.jsp

  1. <html>  
  2. <body>  
  3. <%   
  4. String name=request.getParameter("uname");  
  5. out.print("Welcome "+name);  
  6. session.setAttribute("user",name);  
  7. <a href="showuserinfo.jsp">showuser details</a>  
  8. %>  
  9. </body>  
  10. </html>   

showuserinfo.jsp

  1. <html>  
  2. <body>  
  3.  
  4. <%   
  5. String name=(String)session.getAttribute("user");  
  6. out.print("Hello "+name);  
  7. %>  
  8.  
  9. </body>  
  10. </html> 
  11. %> 

7.pagecontext:

  • pagecontext implicit object is type of  PageContext.
  • pagecontext object used to set , get and remove attributes in particular scope
  1. page scope
  2. request scope
  3. session scope
  4. application scope

  1. pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
  2.  String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE); 

8.page

  • Page implicit variable hold the currently executes Servlet object for the corresponding JSP.
  • Acts as this object for current jsp page
  1.  <%=page.getClass().getName() %>
  2.   <%=page.getServlestInfo()%> 

9.exception

  • The implicit variable can be used only in Error pages. When we try to use this variable in a JSP which is not an Error page we get an Error message.

  1.  <%@ page isErrorPage="true" %>  
  2. <html>  
  3. <body>   
  4.  
  5. <%= exception %>  
  6.  
  7. </body>  
  8. </html> 


Select Menu