Home arrow Java arrow Page 4 - Finishing the Project: Java Web Development in Eclipse and Tomcat
JAVA

Finishing the Project: Java Web Development in Eclipse and Tomcat


Last week, I introduced you to some of the fundamental concepts you need for working with Java web components. For this project we'll be working with JSP and servlets. I walked you through getting the appropriate downloads installed and setting up your work space. We stopped after just creating the web project. In this part, we will add our content, including HTML, JSP, and servlets.

Author Info:
By: Gangyi
Rating: 5 stars5 stars5 stars5 stars5 stars / 24
May 29, 2007
TABLE OF CONTENTS:
  1. · Finishing the Project: Java Web Development in Eclipse and Tomcat
  2. · Create welcome.jsp
  3. · Run the web application in the Eclipse environment
  4. · What just happened?

print this article
SEARCH DEVARTICLES

Finishing the Project: Java Web Development in Eclipse and Tomcat - What just happened?
(Page 4 of 4 )

You must be wondering what happened. How can Welcome aboard <%=username%> dynamically become Welcome aboard user? How are the user name and password entered in the login.html page being carried over to welcome.jsp? This brings us to another important Java web component called Servlet.

First, the parameters entered in the form fields are carried over from login.html to welcome.jsp by the request object.

Secondly, let's navigate from file explore to:

C:JavaWebDev_Tutorials_Workspace.metadata.plugins
org.eclipse.wst.server.
coretmp0workCatalinalocalhostJavaWebDev_Tutorial_1orgapachejsp

(Catalina is another name for version 5 Tomcat)

You'll find welcome_jsp.java and welcome_jsp.class, which are compiled from Java. Those two files are created and managed by the Eclipse environment.

Let's import welcome_jsp.java into Eclipse IDE to see the details.

First, highlight the Java Resources folder, then from the content menu, select import -> File System.

Then browse into the directory above.

Click Finish, and the Java code shows on the Eclipse content panel. Below is the full text; you can instantly recognize that the red portion is from welcome.jsp

<%
 
String secreteUser = "user";
 
String secretePass = "pass";
 
String username = request.getParameter("username");
 
String password = request.getParameter("password");
%>
<%if(!(username.equals(secreteUser) || password.equals(secretePass))) {%>
 
Wrong username / password
<%} else {%>
 
Welcome aboard <%=username%>!
<%}%> '

Now you have figured out the relationship between JSP and Servlet! Let's go a step further to prove that JSP actually is the text form of the Servlet class.

package org.apache.jsp;

import Javax.servlet.*;
import Javax.servlet.http.*;
import Javax.servlet.jsp.*;

public final class welcome_jsp extends org.apache.jasper.runtime.HttpJspBase
 
implements org.apache.jasper.runtime.JspSourceDependent {

  private static Java.util.Vector _jspx_dependants;

  public Java.util.List getDependants() {
   
return _jspx_dependants;
 
}

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
     
throws Java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;
   
PageContext pageContext = null;
   
HttpSession session = null;
   
ServletContext application = null;
   
ServletConfig config = null;
   
JspWriter out = null;
   
Object page = this;
   
JspWriter _jspx_out = null;
   
PageContext _jspx_page_context = null;

    try {
     
_jspxFactory = JspFactory.getDefaultFactory();
     
response.setContentType("text/html");
     
pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true);
     
_jspx_page_context = pageContext;
     
application = pageContext.getServletContext();
     
config = pageContext.getServletConfig();
     
session = pageContext.getSession();
     
out = pageContext.getOut();
     
_jspx_out = out;

      out.write("<html>n");
     
out.write("<head>rn");
      
out.write("n");
     
out.write("<title>Welcome page</title>n");
     
out.write("</head>n");
     
out.write("<body>rn");
     
out.write("<pre>n");

      String secreteUser = "user";
     
String secretePass = "pass";
     
String username = request.getParameter("username");
     
String password = request.getParameter("password");

      out.write('r');
     
out.write('n');
     
if(!(username.equals(secreteUser) || password.equals(secretePass))) {
       
out.write("rn");
       
out.write("Wrong username / passwordrn");
     
} else {
       
out.write("rn");
       
out.write("Welcome aboard ");
       
out.print(username);
       
out.write(" !rn");
     
}
     
out.write("rn");
     
out.write("</pre>n");
     
out.write("</body>n");
     
out.write("</html>");
   
} catch (Throwable t) {
     
if (!(t instanceof SkipPageException)){
       
out = _jspx_out;
       
if (out != null && out.getBufferSize() != 0)
         
out.clearBuffer();
       
if (_jspx_page_context != null)
         
_jspx_page_context.handlePageException(t);
      
}
   
} finally {
       
if (_jspxFactory != null)
         
_jspxFactory.releasePageContext(_jspx_page_context);
   
}
  
}
}

Now let's do a little more work to prove that JSP actually is a Servlet. Modify login.html's form part

<form action="welcome.jsp" method="post">

To

<form action="welcome_jsp" method="post">

Next, expand WebContent -> WEB-INF, and Click web.xml. This is the web application configuration file. Modify the content to the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>JavaWebDev_Tutorial_1</display-name>
 
<servlet>
   
<description></description>
   
<display-name>welcome_jsp</display-name>
   
<servlet-name>welcome_jsp</servlet-name>
   
<servlet-class>org.apache.jsp.welcome_jsp</servlet-class>
 
</servlet>
 
<servlet-mapping>
   
<servlet-name>welcome_jsp</servlet-name>
   
<url-pattern>/welcome_jsp</url-pattern>
 
</servlet-mapping>
</web-app>

The web.xml defines the Servlet we just imported from and the servlet-mapping's url-pattern pointed to the org.apache.jsp.welcome_jsp.welcome_jsp servlet with the short name welcome_jsp corresponding to login.htm action part here:

<form action="welcome_jsp" method="post">

If you run the application again, you'll get the same result.

To explain it further, the welcome_jsp actually extends org.apache.jasper.runtime.HttpJspBase

public final class welcome_jsp extends org.apache.jasper.runtime.HttpJspBase

Here is the class hierarchy:

(http://tomcat.apache.org/tomcat-4.1-
doc/jasper/docs/api/org/apache/jasper/runtime/HttpJspBase.html
)

java.lang.Object
extended byjavax.servlet.GenericServlet
    extended byjavax.servlet.http.HttpServlet
        extended byorg.apache.jasper.runtime.HttpJspBase

So the welcome_jsp is indeed a Servlet and it's generated from welcome.jsp by the Eclipse environment.

Summary

In this short tutorial, we introduced the Integrated Java Web Development environment with Eclipse and how to develop, test, and configure a web application. We are ready to move to the next in upcoming articles. 


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials