|
JEE : Broad Spectrums : JSP, JSF, Servlet : with
NetBean 6.8 IDE |
Web Applications can be broadly branched into three following
essential web-components
- Java Server Pages ( Like PHP and ASP.Net, JSP allows to
embed dynamic elements ( representations of server's injections)
to a HTML-Web-Page.
- Servlets ( capable of receiving Request and generating
Responses to the HTTP clients), in contrast to JSP, a Servlet
method will carry the responses from servers spaced with HTML
tags.
- Web-Services
|
Book marks on this page
|
About Servlets
- Developers tool interact with Business Object like EJB
- Servlet receives a request from a browser via server,
does maneuverings with JDBC to call an EJB component and
sends HTML or XML responses.
- Servlets can be used without EJBs
- Getting responses in HTML or XML format.
- Servlets are compiled objects (unlike JavaScript).
For more information about Servlet technology, see the Java
Servlet Technology Documentation available at
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets4.html

A Servlet

A JSP

|
Testing this examples
(Intranet , replacing server name or as localhost.)
- JSP: index.jsp
http://manas2:8080/serv1/
- Struts
http://manas2:8080/serv1/welcomeStruts.jsp
- Java Server Faces :
http://manas2:8080/serv1/faces/welcomeJSF.jsp
- Servlet :
http://manas2:8080/serv1/faces/TestServlet

|
| Starting fresh |
Step 1. Choose a Project Type
|
| Step 2 Project Name and Location: name and location of the
web project
 |
| Step 3: Project Choose Frameworks:

|
| |
| If you look in the serv1 folder , you would find many folders
created with Net Bean IDE 6.0 build.xml
link 
|
| web folder: Under the hood of web there are three formatted pages
are created index.jsp, welcomeStruts.jsp, welcomeJSF.jsp

|
| Runtime of the other files 


|
| Creating a Servlet File and Class |
Now choose a file type
- In the Projects or Files window, right-click the project's node
and choose New > Other from the pop-up menu. The New File wizard
appears.
- Under Categories, choose Web. Under File Types, choose Servlet.
Click Next.

|
| File : Name and location contd 
|
| configuring Servlet_deployment 
|
| now note

|

Code Used: to use doPost method
package sr1;
import java.io.IOException;
import java.io.PrintWriter;
//import java.util.Enumeration;
import javax.servlet.Servlet;
//import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet implements Servlet {
/**
* Note the option of having ID
*/
private static final long serialVersionUID = 4427008781683943539L;
public HelloWorld() {
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String str1 = "A String Variable";
writer.println("<font style='color:red'>Welcome to Eclipse Servlet
doPost Response :display</font><br/><font style='color:Green';> ->"
+ str1 + "</font><br/>");
}
}
|
| |

|
Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:0000000111
EndFragment:0000001953
Viewing a JSP File's Servlet
See Also
When you execute a JSP file, the server translates the contents
of the JSP file into a Java servlet and compiles the servlet. You
can view, but not edit, the translated servlet source in the Source
Editor. You cannot set breakpoints in the servlet source code. You
can set breakpoints in the JSP file only.
To view the servlet generated from a JSP file:
- Run the JSP file.
- In the Projects window, right-click the JSP file and choose
View Servlet.
|
The View Servlet menu item is enabled only if the
target server has successfully executed the JSP file and
the target server supports the viewing of translated
servlets. |
The servlet appears in the Source Editor. This servlet is based on
the state of the JSP file when the file was last executed. If you
have changed the JSP file but have not executed it, the servlet does
not reflect the changes. While this file is opened, if you execute
the JSP file or if you change the target server in the Run section
of the Project Properties dialog box, the file changes to reflect
the latest translated servlet on the selected server.
|
Run time confirms by opening a blank page

because the codes are blocked.

First undo the quotes and run the file

Now see the fruit of your hard work

|
Now there are two pages of importance
- The main page
- the Servlet page


|
code Finale of the Servlet page:
The code in the Servlet file (final)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serv1pkg;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author Manas
*/
public class TestServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and
<code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here jsp out out
start*/
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body text='#000080' bgcolor='#B4C5F1'>");
out.println("<h1>Servlet TestServlet at " +
request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
/* html page ends here */
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet
methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
|
| The Code of First page that linked to the Servlet page

<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : Page1
Created on : Jan 27, 2008, 1:13:53 AM
Author : Manas
-->
<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
<jsp:directive.page contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"/>
<f:view>
<webuijsf:page binding="#{First_Page.page1}" id="page1">
<webuijsf:html binding="#{First_Page.html1}" id="html1" lang="">
<webuijsf:head binding="#{First_Page.head1}" id="head1" title="This
is the first page of servlet">
<webuijsf:link binding="#{First_Page.link1}" id="link1" url="/resources/stylesheet.css"/>
</webuijsf:head>
<webuijsf:body binding="#{First_Page.body1}" id="body1"
style="background-color: #B4C5F1; background-image: url(<Not Set>);
-rave-layout: grid">
<webuijsf:form binding="#{First_Page.form1}" id="form1"
virtualFormsConfig="">
<webuijsf:hyperlink binding="#{First_Page.hyperlink1}"
id="hyperlink1"
style="height: 22px; left: 360px; top: 96px; position: absolute;
width: 72px" target="_blank" text="Hyperlink" url="http://manas2:8080/serv1/TestServlet"/>
<webuijsf:label binding="#{First_Page.label1}" id="label1"
style="color: red; font-size: 24px; height: 70px; left: 48px; top:
0px; position: absolute; width: 526px" text="this is a page of
project run via index.jsp"/>
<webuijsf:label binding="#{First_Page.label2}" id="label2"
style="color: blue; height: 48px; left: 72px; top: 120px; position:
absolute; width: 504px" text="To view the servlet runt time either
copy this link http://localhost:8080/serv1/TestServlet or use the
Hyperlink to open the servelet"/>
</webuijsf:form>
</webuijsf:body>
</webuijsf:html>
</webuijsf:page>
</f:view>
</jsp:root>
|
Concept Under the hood: web Folder

|
The details on Struts will be extended in another bare-bone
module, the illustration below will give a glimpse of the codes
generated with NetBean IDE. (

|
Folder WEB-INF

code web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
</context-param>
<filter>
<filter-name>UploadFilter</filter-name>
<filter-class>com.sun.webui.jsf.util.UploadFilter</filter-class>
<init-param>
<description>The maximum allowed upload size in bytes. If this is
set to a negative value, there is no maximum. The default value is
1000000.</description>
<param-name>maxSize</param-name>
<param-value>1000000</param-value>
</init-param>
<init-param>
<description>The size (in bytes) of an uploaded file which, if it is
exceeded, will cause the file to be written directly to disk instead
of stored in memory. Files smaller than or equal to this size will
be stored in memory. The default value is 4096.</description>
<param-name>sizeThreshold</param-name>
<param-value>4096</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UploadFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<init-param>
<param-name>javax.faces.LIFECYCLE_ID</param-name>
<param-value>com.sun.faces.lifecycle.PARTIAL</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ExceptionHandlerServlet</servlet-name>
<servlet-class>com.sun.errorhandler.ExceptionHandler</servlet-class>
<init-param>
<param-name>errorHost</param-name>
<param-value>localhost</param-value>
</init-param>
<init-param>
<param-name>errorPort</param-name>
<param-value>24444</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>ThemeServlet</servlet-name>
<servlet-class>com.sun.webui.theme.ThemeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>serv1pkg.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ExceptionHandlerServlet</servlet-name>
<url-pattern>/error/ExceptionHandler</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ThemeServlet</servlet-name>
<url-pattern>/theme/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/First_Page.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error/ExceptionHandler</location>
</error-page>
<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/error/ExceptionHandler</location>
</error-page>
<error-page>
<exception-type>javax.faces.FacesException</exception-type>
<location>/error/ExceptionHandler</location>
</error-page>
<error-page>
<exception-type>com.sun.rave.web.ui.appbase.ApplicationException</exception-type>
<location>/error/ExceptionHandler</location>
</error-page>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<jsp-property-group>
<url-pattern>*.jspf</url-pattern>
<is-xml>true</is-xml>
</jsp-property-group>
</jsp-config>
</web-app>
|
| |
| |
| |