Applet vs' Servlet - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Applet vs' Servlet

Description:

Security restrictions can be managed using policy tool ... internet applications. Use scripting for data validation, animated GIFs for animation, use forms and ... – PowerPoint PPT presentation

Number of Views:218
Avg rating:3.0/5.0
Slides: 16
Provided by: ucd76
Category:

less

Transcript and Presenter's Notes

Title: Applet vs' Servlet


1
Applet vs. Servlet
  • Use Java Plug-In to deliver applets for intranet
    applications.
  • Security restrictions can be managed using policy
    tool to list trusted sites.
  • Downloaded classes are usually measured in
    megabytes, OK over high-speed internet
    connections.
  • Java Plug-In installs the approriate JDK version
    OK for corporate users.
  • Dont use applets for internet applications. Use
    scripting for data validation, animated GIFs for
    animation, use forms and server-side processing
    (whether they are traditional CGI scripts, Java
    servlets, or server side scripting languages) for
    data entry.

2
J2EE Applications Programming
  • Application
  • Applet
  • Remote Method Invocation
  • Java Bean
  • Enterprise Java Bean
  • Servlet
  • Java Server Page

3
Thin Client Multitiered Architecture
4
J2EE Platform
5
Bonus Calculation Form
6
Bonus.html
  • ltHTMLgt
  • ltBODY BGCOLOR "WHITE"gt
  • ltBLOCKQUOTEgt
  • ltH3gtBonus Calculationlt/H3gt
  • ltFORM METHOD"POST" ACTION"servlet/mypackage.Bonu
    sServlet"gt
  • ltPgt
  • Enter social security Number
  • ltPgt
  • ltINPUT TYPE"TEXT" NAME"SOCSEC"gtlt/INPUTgt
  • ltPgt
  • Enter Multiplier
  • ltPgt
  • ltINPUT TYPE"TEXT" NAME"MULTIPLIER"gtlt/INPUTgt
  • ltPgt
  • ltINPUT TYPE"SUBMIT" VALUE"Submit"gt
  • ltINPUT TYPE"RESET"gt
  • lt/FORMgt
  • lt/FORMgt
  • lt/BLOCKQUOTEgt

7
BonusServlet Output
8
BonusServlet
  • Context ctx null
  • // Retrieve EJB
  • try
  • ctx new InitialContext()
  • Object homeObject ctx.lookup("GerlachEmp")
  • EmpHome empHome (EmpHome)
  • PortableRemoteObject.narrow(homeObject,EmpHome
    .class)
  • Emp emp empHome.findByPrimaryKey(new
    EmpPK(ssn))
  • // Output Calculation -- Use EJB
  • out.println("ltPgtSalary Amount retrieved "
    emp.getSal() "ltPgt")
  • out.println("ltPgtMultiplier " multiplier
    "ltPgt")
  • out.println("ltPgtBonus Calculation "
    emp.getSal() multiplier "ltPgt")
  • out.println("lt/BODYgtlt/HTMLgt")
  • // end of try block
  • catch(Throwable ex)
  • out.println("ltPgtNo Such EmployeeltPgt")
  • public class BonusServlet extends HttpServlet
  • public void init(ServletConfig config) throws
    ServletException
  • public void doPost (HttpServletRequest request,
    HttpServletResponse response) throws
    ServletException, IOException
  • doGet (request, response)
  • public void doGet (HttpServletRequest request,
    HttpServletResponse response) throws
    ServletException, IOException
  • String socsec null
  • int multiplier 0
  • double calc 0.0
  • PrintWriter out
  • response.setContentType("text/html")
  • String title "EJB Example"
  • out response.getWriter()
  • out.println("ltHTMLgtltHEADgtltTITLEgt")
  • out.println(title)
  • out.println("lt/TITLEgtlt/HEADgtltBODYgt")
  • //Retrieve User Input

9
Create and Run Servlet
  • File New Html to create html page to launch
    servlet
  • File New HTTP Servlet to create servlet class
  • Code the servlet to access local EJBs (cannot run
    servlet locally with remote EJBs).
  • Link the .html page to the servlet by specifying
    it's path name in the ACTION tag.
  • Right click on html page and select run (or
    debug) in browser to execute. JDeveloper will
    start your EJBs automatically -- Do not start
    EJBs yourself.
  • Terminate the process before retesting!

10
Get and Post Methods
  • doGet technique encodes user input values in the
    URL (CGI script support)
  • doPost technique places the user input values
    into the body of the request
  • Code doGet and doPost methods in the exact same
    way doGet can call doPost or vice versa.
  • The method used (doGet or doPost) is determined
    by the METHOD tag METHOD GET or METHOD POST.

11
Conditional Servlet Invocation
  • Provide code in the servlet to create alternative
    HTML pages.
  • Code the servlet to select a static html page as
    a response. (Valid for Get requests only)
  • RequestDispatcher rd getServletContext().getRequ
    estDispatcher("ssnerror.html")
  • rd.forward(request, response)
  • This option did not seem to want to work in
    JDeveloper
  • Code the servlet to redirect the request and
    response to another servlet or html page.
  • response.sendRedirect("/servlet/mypackage.BonusSe
    rvlet2Calc")
  • response.sendRedirect("/ssnerror.html")
    response.sendRedirect("http//132.194.120.1967070
    /Servlet_html/ssnerror.html")

12
Multiple Buttons
  • Bonus2.Html page specification of buttons
  • ltINPUT TYPE"SUBMIT" NAME "SUBMITBUTTON"
    VALUE"Submit"gt
  • ltINPUT TYPE"SUBMIT" NAME "CANCELBUTTON"
    VALUE"Cancel"gt
  • BonusServlet2 checks button type in doGet
  • String cancel request.getParameter("CANCELBUTTON
    ")
  • if(cancel!null)
  • if (cancel.equals("Cancel"))
  • response.sendRedirect("/Cancel.html")
  • return
  • String submit request.getParameter("SUBMITBUTTON
    ")
  • if (submitnull)
  • response.sendRedirect("/Cancel.html")
  • return

13
Accessing Session Variables
  • BonusServlet2doGet supplies Emp object
  • EmpHome empHome (EmpHome)ctx.lookup("GerlachEmp"
    )
  • Emp emp empHome.findByPrimaryKey(new
    EmpPK(ssn))
  • HttpSession session request.getSession(true)
  • session.setAttribute("employee", emp)
  • session.setAttribute("multiplier", new
    Integer(multiplier))
  • response.sendRedirect("/servlet/mypackage.BonusSer
    vlet2Calc")
  • BonusServlet2CalcdoGet uses Emp object
  • HttpSession session request.getSession(false)
  • Emp emp Integer I int multiplier
  • try
  • if (session ! null)
  • emp (Emp) session.getValue("employee")
  • I (Integer) session.getValue("multiplier")
  • multiplier I.intValue()
  • out.println("ltPgtSoc Sec " emp.getEmpno()
    "ltPgt")
  • out.println("ltPgtSalary Amount retrieved "
    emp.getSal() "ltPgt")
  • out.println("ltPgtMultiplier " multiplier
    "ltPgt")
  • out.println("ltPgtBonus Calculation "
    emp.getSal() multiplier "ltPgt")

14
GUI Components for Forms
  • Handout

15
Application Design
  • Initial user screen is an html page.
  • The initial user screen calls the first servlet
    when user hits the submit button.
  • The reply html page produced by the first servlet
    will specify a second servlet in the METHOD tag.
  • When the user depresses the submit button of the
    reply html page, the second servlet is invoked.
    Servlet two creates yet another reply page that
    is associated with a third servlet. And so the
    processing goes.
Write a Comment
User Comments (0)
About PowerShow.com