Files

Document Sample
Files Powered By Docstoc
					       Lecture 5


Servlets and JavaServer Pages
             (JSP)
Servlet?
   A Servlet is a Java class that provides
    special server side service.
   Servlets runs in a server and returns
    answer to clients in a web page.
   Servlets build webpages....so, it’s
    contains HTML
Structure
import java.io.*;
    import java.net.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Welcome extends HttpServlet {
     public void doGet( HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

           response.setContentType("text/html; charset=UTF-8");
           PrintWriter out = response.getWriter();
           String message1=request.getParameter("Order")
           //TODO output your page here
           out.println("<html>");
           out.println("<head>");
           out.println("<title>order</title>");
           out.println("</head>");
           out.println("<body>");
           out.println("<h1><strong>I like to order <br>Get Order Servlet </h1>");
           out.println("</body>");
           out.println("</html>");
           out.close();
       }
How it’s works



public void doGet( HttpServletRequest request,

 HttpServletResponse response)

 { …………….. }
 -HTLM forms let the user submit data to romote size
 - Servlets use HTTP proprieties, request , respons to
comunicate
-Request, incapsulate data from the client
-Respons, incapsulate the response information from the server
   JSPs are essential an HTML
    page with special JSP tags
    embedded.
   These JSP tags can contain
    Java code.
   The JSP file extension is
    .jsp rather than .
   The JSP engine parses the
    .jsp and creates a Java
    servlet source file.
   HTML from the Servlet
    output is sent via the
    Internet.
   HTML results are displayed
    on the user's web browser.
Structure
<html>
<head>
<title>My first JSP page
</title>
</head>
<body>
<%@ page language="java" %>
<% out.println("Hello World"); %>
</body>
</html>
Using JSP tags



   Declaration tag
   Expression tag
   Directive tag
   Scriptlet tag
   Action tag
1) Declaration tag ( <%! %> ) . This tag allows the developer to
   declare variables or methods.
<%!
      private int counter = 0 ;
      private String get Account ( int accountNo) ;
%>
2) Expression tag ( <%= %>) This tag allows the developer to
   embed any Java expression and is short for out.println().

Date : <%= new java.util.Date() %> will print the date
3) Directive tag ( <%@ directive ... %>)

A JSP directive gives special information about the page to the JSP Engine.
Directives do not produce any visible output when the page is requested but
change the way the JSP Engine processes the file.

There are three main types of directives:

1)   page - processing information for this page. <%@ page language = "java" %>

2)   Include - files to be included. <%@ include file = "include/privacy.html" %>

3)   Tag library - tag library to be used in this page. <%@ taglib uri = "tag library

URI" prefix = "tag Prefix" %>
4) Scriptlet tag ( <% ... %> )
Between <% and %> tags,any valid Java code is called a Scriptlet..
<%
     String username = "visualbuilder" ;
     out.println ( username ) ;
 %>


5) Action tag

 There are three main roles of action tags :

1)   enable the use of server side Javabeans
2)   transfer control between pages
3)   browser independent support for applets.
   JSP compared to ASP

    JSP and ASP are fairly similar in the functionality that they provide. JSP may
    have slightly higher learning curve. Both allow embedded code in an HTML
    page,session variables and database access and manipulation. Whereas ASP
    is mostly found on Microsoft platforms i.e. NT,JSP can operate on any platform
    that conforms to the J2EE specification. JSP allow component reuse by using
    Javabeans and EJBs. ASP provides the use of COM / ActiveX controls.

   JSP compared to ASP.NET
    ASP.NET is based on the Microsoft .NET framework. The .NET framework
    allows applications to be developed using different programming languages
    such as Visual Basic,C# and JavaScript. JSP and Java still has the advantage
    that it is supported on many different platforms and the Java community has
    many years of experience in designing and developing Enterprise quality
    scalable applications. This is not to say that ASP.NET is bad,actually it is quite
    an improvement over the old ASP code.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:9
posted:8/12/2012
language:English
pages:12