Servlet and JSP - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Servlet and JSP

Description:

throws ServletException, IOException { res.setContentType('text/html' ... throws javax.servlet.ServletException, java.io.IOException { performTask(request, response) ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 15
Provided by: sra8
Category:

less

Transcript and Presenter's Notes

Title: Servlet and JSP


1
Servlet and JSP -exercises-
Alessandro Marchetto Fondazione Bruno
Kessler-IRST, Centro per la Ricerca Scientifica
e Tecnologica
2
HelloWorld
- Write a Servlet that writes Hello Client!
3
HelloWorld servlet (1)
...servlet code...
import java.io. import javax.servlet. import
javax.servlet.http. public class
HelloClientServlet extends HttpServlet
protected void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
res.setContentType("text/html") PrintWriter
out res.getWriter() out.println("ltHTMLgtltH
EADgtltTITLEgtHello Client!lt/TITLEgt"
"lt/HEADgtltBODYgtHello Client!lt/BODYgtlt/HTMLgt")
out.close()
4
HelloWorld servlet (2)
...web.xml...
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt!DOCT
YPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http//java.sun.com/dtd/web-app_2_3.dtd"gt
ltweb-appgt ltservletgt ltservlet-namegtHelloClientSer
vletlt/servlet-namegt ltservlet-classgtHelloClientServ
letlt/servlet-classgt lt/servletgt ltservlet-mappinggt
ltservlet-namegtHelloClientServletlt/servlet-namegt ltu
rl-patterngt/HelloClientServletlt/url-patterngt lt/ser
vlet-mappinggt lt/web-app
5
JSP demo
  • Write a JSP application composed of
  • An HTML page with a form that requests a string
    to the user (e.g., name)
  • A JSP page that
  • it reads the string sent by the client html
  • and it puts the string on output (such as
    Welcomestring)
  • - it puts on the output also the current date
    (using java.util.Date)

6
JSP demo
...html code...
ltform id"f1" method"get" action"demo2.jsp"gt
Name ltinput type"text" name"param1" /gt
ltinput type"submit" value"submit to jsp"
/gt lt/formgt
...jsp code...
lt_at_ page import"java.util." gt lthtmlgt
ltbodygt lt String name(String)request.getParamet
er("param1")gt ltpgtWelcome, lt
out.print(name)gt Today is lt Date d new
java.util.Date() out.print(d) gt lt/pgt
lt/bodygt lt/htmlgt
7
Servlet demo
  • Write a servlet that is able to write This is a
    Simple Servlet Demo
  • The servlet needs
  • to extends the HttpServlet class
  • to override both the doGet and doPost methods of
    the class HttpServlet

8
Servlet demo (1)
...servlet code...
import java.io.PrintWriter public class
SimpleServlet extends javax.servlet.http.HttpServl
et public void doGet(javax.servlet.http.HttpSe
rvletRequest request, javax.servlet.http.H
ttpServletResponse response) throws
javax.servlet.ServletException,
java.io.IOException performTask(request,
response) public void doPost(javax.servlet.h
ttp.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException,
java.io.IOException performTask(request,
response) public void performTask(javax.serv
let.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
try response.setContentType("text/html")
PrintWriter out response.getWriter()
out.println("This is a Simple Servlet Demo")
catch(Throwable theException)
//theException.printStackTrace()
9
Servlet demo (2)
...web.xml...
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
web-app PUBLIC "-//Sun Microsystems, Inc.//DTD
Web Application 2.2//EN" "http//java.sun.com/j2ee
/dtds/web-app_2_2.dtd"gt ltweb-appgt ltservletgt ltser
vlet-namegtSimpleServletlt/servlet-namegt ltservlet-cl
assgtSimpleServletlt/servlet-classgt lt/servletgt ltser
vlet-mappinggt ltservlet-namegtSimpleServletlt/servlet
-namegt lturl-patterngt/SimpleServletlt/url-patterngt lt
/servlet-mappinggt lt/web-app
10
Servlet and Form
  • Write a servlet that is able to read three inputs
    and write them in output.
  • Client
  • an HTML page with a Form that contains three
    text fields and a submit button (that uses the
    GET method)
  • Server
  • a servlet that reads the three inputs submitted
    via GET by the Form
  • and write a string composed of the value of the
    three inputs readed

11
Servlet Form1 (1)
...form of the html page...
ltform method"get" action"threeParams"gt First
Par ltinput type"text" name"param1" /gt ltbr
/gt Second Par ltinput type"text" name"param2"
/gt ltbr /gt Third Par ltinput type"text"
name"param3" /gt ltbr /gt ltinput type"submit"
value"Submit" /gt lt/formgt
12
Servlet Form1 (2)
...servlet code...
import java.io. import javax.servlet. import
javax.servlet.http. public class threeParams
extends javax.servlet.http.HttpServlet public
void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException
response.setContentType("text/html") PrintWrite
r out response.getWriter() String title
"Reading Three Request Parameters" out.println(S
ervletUtilities.headWithTitle(title)
"ltBODYgt\n" "ltH1 ALIGNCENTERgt"
title "lt/H1gt\n"
"ltULgt\n" " ltLIgtparam1 "
request.getParameter("param1") "\n"
" ltLIgtparam2 "
request.getParameter("param2") "\n"
" ltLIgtparam3 "
request.getParameter("param3") "\n"
"lt/ULgt\n" "lt/BODYgtlt/HTMLgt")
13
Servlet Form1 (3)
...java class used by the servlet...
public class ServletUtilities public static
final String DOCTYPE "lt!DOCTYPE HTML PUBLIC
\"-//W3C//DTD HTML 4.0 "
"Transitional//EN\"gt" public static String
headWithTitle(String title) return(DOCTYPE
"\n" "ltHTMLgt\n" "ltHEADgtltTITLEgt" title
"lt/TITLEgtlt/HEADgt\n")
14
Servlet Form1 (4)
...web.xml...
lt!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN"
"http//java.sun.com/j2ee/dtds/web-app_2_2.dtd"gt
ltweb-appgt ltservletgt ltservlet-namegtthreeParamslt/se
rvlet-namegt ltservlet-classgtthreeParamslt/servlet-cl
assgt lt/servletgt ltservlet-mappinggt ltservlet-namegtt
hreeParamslt/servlet-namegt lturl-patterngt/threeParam
slt/url-patterngt lt/servlet-mappinggt lt/web-appgt
Write a Comment
User Comments (0)
About PowerShow.com