Servlets

Disponível somente no TrabalhosFeitos
  • Páginas : 6 (1291 palavras )
  • Download(s) : 0
  • Publicado : 20 de setembro de 2012
Ler documento completo
Amostra do texto
© 2010 Marty Hall

Servlet Basics
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/csajsp2.html
Customized Java EE Training: http://courses.coreservlets.com/
2

Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues oronsite at your location.

© 2010 Marty Hall

For live Java EE training, please see training courses
at http://courses.coreservlets.com/.
Servlets, JSP, Struts, JSF 1.x, JSF 2.0, Ajax (with jQuery, Dojo,
Prototype, Ext-JS, Google Closure, etc.), GWT 2.0 (with GXT),
Java 5, Java 6, SOAP-based and RESTful Web Services, Spring,
Hibernate/JPA, and customized combinations of topics.

Taughtby the author of Core Servlets and JSP, More
Servlets
Servlets and JSP, and this tutorial. Available at public
Available
venues,ustomized Java EE Training: http://courses.coreservlets.com/ at your
C or customized versions can be held on-site
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
organization. Contact hall@coreservlets.com fordetails.
Developed and taught by well-known author and developer. At public venues or onsite at your location.

Agenda








The basic structure of servlets
A simple servlet that generates plain text
A servlet that generates HTML
Servlets and packages
Some utilities that help build HTML
The servlet life cycle
Servlet debugging strategies

4

A Servlet’s Job
• Readexplicit data sent by client (form data)
• Read implicit data sent by client
(request headers)
• Generate the results
th
• Send the explicit data back to client (HTML)
• Send the implicit data to client
th
(status codes and response headers)

5

A Servlet That Generates Plain
Text (HelloWorld
Text (HelloWorld.java)
import java.io.*;
import javax
import javax.servlet.*;
importjavax.servlet.http.*;
public class HelloWorld extends HttpServlet {
class HelloWorld extends HttpServlet
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ServletException, IOException
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
URL assumes you have deployed from
an Eclipse project named “intro”.Code
was in src/HelloWorld.java
6

A Servlet That Generates HTML
• Tell the browser that you’re sending it HTML
– response.setContentType("text/html");

• Modify the println statements to build a
legal Web page
legal Web page
– Print statements should output HTML tags

• Check your HTML with a formal syntax
your HTML with formal syntax
validator
– http://validator.w3.org/
–http://www.htmlhelp.com/tools/validator/

7

A Servlet That Generates HTML
(Code)

8

public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
void doGet(HttpServletRequest request
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
HTMLPUBLIC
HTML 4.0
"\n";
out.println(docType +
"\n" +
"Hello\n"+
"\n" +
"Hello\n" +
"");
}
}

A Servlet That Generates HTML
(Result)

Assumes project named intro. Code in src/HelloServlet.java.

9

Using Packages
• Create a package
– R-click on src folder
– New  Package

• Dropping servlet in package
servlet package
– Copy/paste from filesystem or existing project
–Eclipse will automatically change the package statement
at the top of the
at the top of the .java file
file

• Development strategy
– Start with existing servlets and use them as the starting
points for later servlets
• Start with HelloServlet2 at beginning

– Always use packages
– Do not do New  Servlet
• Results in ugly code with unnecessary parts
10

Using Packages (Continued)
•...
tracking img