Showing posts with label Servlets. Show all posts
Showing posts with label Servlets. Show all posts

Saturday, July 2, 2011

JSP Servlet HTML Communication

When you are working with jsp & servlets you need to go from one page to another & that page may be a jsp, servlet, java page or html page. Well I know how I suffered from those basic stuff when I am at the basics. So to   make you feel better let me give some hints on those conversions.

  • HTML to Sevlet
< form action ="/newservlet" method ="post" />
Here the form action will be the page you need to go after a submit. Here the servlet page is newservlet.java.
  •  JSP to Servlet
<jsp:include page="/newservlet.java" Flush="true" />
This jsp:include will make a jsp page request to go to a servlet & do the logic within the request & return the values will be return to the jsp page again.
  • JSP to JSP
<jsp:forward page="/newjsp.jsp" />
The jsp:forward command will redirect the current page to another page (newjsp.jsp).
  • Servlet to JSP/HTML
String des = "/newjsp.jsp";
response.SendRedirect(response.encodedRedirect(des));

Another way.
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/testin.html");
Dispatcher.include(request,response);

So hope you find this post helpful for you to begin work. :)


JSP & Servlets

Java Servlets

Servlets are Java technology's answer to CGI programming. They are programs that run on a Web server and build Web pages. Building Web pages on the fly is useful (and commonly done) for a number of reasons:
  • The Web page is based on data submitted by the user. For example the results pages from search engines are generated this way, and programs that process orders for e-commerce sites do this as well.
  • The data changes frequently. For example, a weather-report or news headlines page might build the page dynamically, perhaps returning a previously built page if it is still up to date.
  • The Web page uses information from corporate databases or other such sources. For example, you would use this for making a Web page at an on-line store that lists current prices and number of items in stock.
Servlets don't have a main method & they are under the control of a container. Server sends the request to the container & then the container call for the correct servlet.
In order to get the service of a servlet you need to import following containers to your java file. 
import javax.servlet.http.* 
import javax.servlet.*

A Servlet has two methods.
Http.ServletRequest - this object is passed as arguments to servlets's service methods such as doGet, doPost
Http.ServletResponse - this object is passed as arguments to servlets's service methods such as doGet, doPost.


Java Server Pages

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML. Many Web pages that are built by CGI programs are mostly static, with the dynamic part limited to a few small locations. But most CGI variations, including servlets, make you generate the entire page via your program, even though most of it is always the same. JSP lets you create the two parts separately.
 Here's an example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Welcome to Our Store</TITLE></HEAD>
<BODY>
<H1>Welcome to JSP</H1>
<SMALL>Welcome,
<!-- User name is "New User" for first-time visitors --> 
<% out.println(Utils.getUserNameFromCookie(request)); %>
To access your account settings, click
<A HREF="Account-Settings.html">here.</A></SMALL>
<P>
Regular HTML for all the rest of the on-line store's Web page.
</BODY></HTML>
In the next post let me tell you about JSP/Servlet communication. Stay tuned :) 



Introduction to J2EE

J2EE or Java 2 Platform, Enterprise Edition is one of the most powerful ways of developing Multitiered web applications. The J2EE platform uses a distributed multitiered application model for enterprise applications. Application logic is divided into components according to function, and the various application components that make up a J2EE application are installed on different machines depending on the tier in the multitiered J2EE environment to which the application component belongs. multitiered application can be describes as in the figure below.

As this is about web application development, there is always two sides as Client & Server. In the client side the java pages, html pages are running & in the server side, Java Server Pages (JSP) , Servlets, Enterprise Java Beans (EJB) are running. So tiers made them separate from each other & also give a clear way to communicate each other.

J2EE Containers

The component-based and platform-independent J2EE architecture makes J2EE applications easy to write because business logic is organized into reusable components. In addition, the J2EE server provides underlying services in the form of a container for every component type. Because you do not have to develop these services yourself, you are free to concentrate on solving the business problem at hand.So the container is doing the controlling part of the web components in the web development. Container is the interface between a component & its functional behavior. So in order to execute an application the container should be loaded before hand.
the relationship between container & J2EE can be shown as follows.

There are mainly 5 containers available in J2EE, namely
J2EE Server , Enterprise JavaBeans (EJB) container, Web container, Application client container & Applet container.

Web Service Support

The J2EE platform provides the XML APIs and tools you need to quickly design, develop, test, and deploy web services and clients that fully interoperate with other web services and clients running on Java-based or non-Java-based platforms. Basically J2EE has XML , WSDL , SOAP & UDDI standards support in order to have a complete web service application development environment.

So this is just a brief description of J2EE environment. In the next posts, I'm hoping to tell more about JSP, Servlets & its implementation process in action. Stay tuned until then ;)