Title: Java Servlets
1????????
????????? ???????
??????? ?????
Java Web Developer Acsior Ltd.
?????????? ???????? ?? ?????????? ?? ???????
www.acsior.com
academy.devbg.org
2??????????
- ??????? ?? ???????????? ?? ??????????
- ????? ? ????????
- ?????? ?? ??????????
- ???? ?? ?????????? ?????????
- Time Servlet ??????
- ?????????? ?? ???????? ? NetBeans IDE
- ??????????? ?? ??????????
- API ??????????? ?? ??????????
- ?????? ????? ?? ????????
3?????????? (2)
- ???????? ????????
- ????????? ?? ????????? Hello Servlet
- Image Counter Servlet
- ?????????? ?? ?????
- ????? ? ??????
- API ??????????? ?? ???????????? ?? ?????
- ???????? ?? ?????
4?????????? (3)
- ??????? ??? ?????
- Login / Logout ??????????
- ???????? ? ???? ?? ????????
5????????
??????? ?? ????????????
6????? ? ????????
- Java ?????????? ??
- ?????????? ?? ?????????? ?? ????????? ???
???????? (???? PHP, ASP, ASP.NET, ...) - ?????????? ?? ??????????? ? ????????? ????????
??????????, ???????? ?? Java, ????? ??????????
???????????? ??? ??????? - Java ????????, ??????????? HTTP ??????
- ?????? HttpServlet
- ?????????? ????????? ?????????? ?? ??????????
(HTML, XML, )
7????? ? ???????? (2)
- ????????
- ??????????? ??? framework ?? ?????? ???????? ??
?????? ??????-??????? - ????????? ?? ????? Java ???????? ??????
- ???? ?????? ?? ?????? ??????? ?? Java ? Java EE
API ?????????? - JDBC, Persistence, EJB, JMS, JAX-WS, JTA, JTS,
RMI, JNDI, JAXP, ... - ??????? ???? ?? ?????? Java web application
?????????? (JSP, JSF, ...)
8?????? ?? ??????????
- ?????????? ??????????? ????? ??????? ??????
- ??????????? API ?????????? ?? ????? ???? ??
?????????? ?? ???????? ?????? - ?????? ???? ?????? ?? JavaServer Pages (JSP) ?
JavaServer Faces (JSF) ???????????? - ????? ?? ??????????? ???????????? ?????? ????? ??
??????? ? ?? ? ?????? - XML, HTML, WML, GIF ? ?.?.
- ????? ?? ?????? ???? Controller ?? JSP/Servlet
????????????
9???? ?? ?????????? ?????????
- ?????????
- ????? ??????, ??????????? ?? ?????????
- ?????
- ????? ?? ?? ?????????? ?? ?????? ?????????? ??
Java ??????????? - ?????????
- ??????? ??????? ?? ?? ????????????
- ????????? ? ?????????
- ?????? ????????????
10???? ?? ?????????? ?????????
- ?????????
- ????? ??????????
- ?????????? ?? ???????
- ??????????
- ?????????? ?? ????? ???????? ??? ???????
- ???????????? ? ?????????
- ?????????? ?? ??????????? ?? ????? ?????
??????????, ? ? ?????? ????? ???????????? ??
?????? ? HTTP ????????? - ??????? ????????? ?? ???????? (SSI,
servlet-chaining, ?????? ? ?.?.)
11?????? Time Servlet
- import java.io.
- import javax.servlet.
- import javax.servlet.http.
-
- public class TimeServlet extends HttpServlet
- public void doGet(HttpServletRequest aRequest,
- HttpServletResponse aResponse)
- throws ServletException, IOException
- PrintWriter out aResponse.getWriter()
- out.println("ltHTMLgt")
- out.println("The time is "
- new java.util.Date())
- out.println("lt/HTMLgt")
-
12?????????? ?? ???????? ? NetBeans IDE
- ????????? ???? Web ??????????
13?????????? ?? ???????? ? NetBeans IDE (2)
- ???????? ??? ??????? ??? Web ????????????
14?????????? ?? ???????? ? NetBeans IDE (3)
15?????????? ?? ???????? ? NetBeans IDE (4)
16Time Servlet
????????????
17????????
???????????
18??????????? ?? ??????????
- ?????? HttpServlet
- ???????? HTTP ?????? ?? ???????
- ?? ????? ?? HTTP ???????? GET, POST ? ?????, ?
????????? ????????? ????? - doGet() ????????? HTTP GET ??????
- doPost() ????????? HTTP POST ??????
- doPut(), doHead(), doDelete(), doTrace(),
doOptions() - ????????? ?????????? ??????????? ???? ?? ???????
??? ?????? ??? ?????? service()
19??????????? ?? ??????????(2)
- ??????? HttpServletRequest
- ??????? ??????? ?? ???????? ?? ???????
- ?????????? ????? ?? HTTP ????????
- ????? ?? ????? ? ????????? ?? ????????
- ????? ????? ?? ??????? (?????????, ??? ? ?.?.)
- ??????? HttpServletResponse
- ????????? ????? ????????? ??????? ?? ???????
- ???????? ????? ?? HTTP ???????? (content type,
????????? ? ?????) - ???? ?? ???????? (???? OutputStream)
20??????????? ?? ??????????(3)
- HTTP GET ??????? ?? ???????? ??????
- ??????????? ?? ???????? ?? ??????? ??????????? ??
??????? - ???????????? ????? ?? ??????? ? ?????
- ?????? ?? ???????? ???????? ?? ???????? ?
bookmarks - HTTP POST ??????? ?? ???????? ??????
- ??????????? ?? ???????? ??????? ??????????? ??
???????, ???????? ????? ?? ????? ? ???? ????? - ???????????? ????? ?? ??????? ? ??????
- ???????????? ?? ??????? ?? ?????? ?? ? ? ?????? ?
URL-?? (???????? ??????)
21API ??????????? ?? ??????????
- ???-??????? ?????????????? ?? ??????????
- ?????????? ?? ????????? ?? ????? (GET ? POST
?????????) - ?????????? ?? ???????????????? ????????? ??
??????? - ?????????? ?? ?????????? ?? ?????????? ???? ??
??????
HttpServletRequest.getParameter(String)
ServletConfig.getInitParameter()
HttpServletRequest.getHeader(String)
22API ??????????? ?? ?????????? (2)
- ???????? ?? header / content type ?? HTTP
???????? - ?????????? ?? ??????? ????? ?? ????????
- ?????????? ?? ??????? ????? ?? ????????
- ???????????? ?? HTTP ?????? ??? ???? URL
HttpServletResponse.setHeader(ltnamegt, ltvaluegt) /
HttpServletResponse.setContentType(String)
HttpServletResponse.getWriter()
HttpServletResponse.getOutputStream()
HttpServletResponse.sendRedirect()
23?????? ????? ?? ???????
- ??? ??????????? ????????? ???????? ????? ??
??????????? ?? ?????????? - ???????? ?? ???? ????? ?? ?????? ?? ?? ????????
?? ????? ???
- ????? ?? ????????????? ???? ?????? ? ?????????
?????????? ?? HttpServlet, ?? ?? ???????????
??????????? ?? ???????? ? ?????????, ?? ?????
??????
24??????? init()
- ??????? ?? ?? ??? ?????????? ?????? ????????? ??
??????? ?? ????? ??? - ?????????????? ?? ?????????? ?????????, ??
????????? ???? ?? ????????? ?????? ?????
???????????? ?? ?????? init(). - ?????????????? ?????? init() ??????
- ?????? ?? ????????? ?????????? ?? ????????
???????, ????? ?? ?????????? ?? ????????? ??
??????????????? ?????? - ????? ????? ?? ????????????? ?? ??????????? ??
????????
25??????? service()
- ??????? ?? ?? ??? ??????????, ?? ?? ????????
????? ????????????? ?????? - ??????? ???????? doGet(), doPost() ? ?.?. ?
?????????? ?? ?????? ?? HTTP ???????? (GET, POST
? ?.?.) - ??????? ????????? ???? ???????
- ?????????? ???? ????? ?? ????????????? ???? ?????
26??????? destroy()
- ??????? ?? ?? ??? ?????????? ?????? ?? ????????
??????????? ?? ???????? - ?????????????? ?? ?????????? ?????????, ?? ??????
?????? ?? ????? ?????????? ????? ??????????? ??
???? ????? - ?????????????? ?????? destroy() ??????
- ?????? ?? ?????????? ?????????? ?? ????????
???????, ????? ??? ???????? ? ?????? init() - ?????? ?? ???????? ??????????? ?? ????????
27Java ????????
???????
28????????? ?? ????????? Hello Servlet
- ?????? ?? ???????? ???????, ????? ?????? ??
????????? ????????????? ??? ? ????? "Hello,
lt?????????????_???gt" - ??????? ?? ?? HTML ????? ? ???????? ????
- ?? ????? ????????? ???? ?? ??????? ??????????
???????? ? ??????
ltform method"GET or POST" action"the servlet"gt
ltinput type"text" name"user_name"gt lt/formgt
String name request.getParameter("user_name")
29?????? Hello Servlet
HelloForm.html
- lthtmlgtltbodygt
- ltform method"GET" action"HelloServlet"gt
- Please enter your name
- ltinput type"text" name"user_name"gt
- ltinput type"submit" value"OK"gt
- lt/formgt
- lt/bodygtlt/htmlgt
HelloServlet.java
import java.io. import javax.servlet.
import javax.servlet.http. public class
HelloServlet extends HttpServlet
30?????? Hello Servlet
HelloServlet.java
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException
response.setContentType("text/html")
ServletOutputStream out response.getOutputSt
ream() String userName
request.getParameter("user_name")
out.println("lthtmlgtltheadgt")
out.println("\tlttitlegtHello Servletlt/titlegt")
out.println("lt/headgtltbodygt")
out.println("\tlth1gtHello, " userName
"lt/h1gt") out.println("lt/bodygtlt/htmlgt")
31????????? ?? ??????? ? NetBeans IDE
- ????????? ?? ???? HTML ?????
32????????? ?? ??? ??????? ? NetBeans IDE
33Hello Servlet ? ????????
34Hello Servlet
????????????
35Hello Servlet HTTP ??????
- ????? ?? ?????? ?????? ???????????? ?????? ?????
??? - Internet Explorer (IE) ??????? ???????? HTTP
?????? ??? Tomcat
GET /FirstWebApp/HelloServlet?user_nameNakov
HTTP/1.1 Accept image/gif, image/x-xbitmap,
image/jpeg,image/pjpeg, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash,
/ Accept-Language bg Accept-Encoding gzip,
deflate User-Agent Mozilla/4.0 (compatible MSIE
6.0 Windows NT 5.1 Q312461) Host
nakov8084 Connection Keep-Alive
36Hello Servlet HTTP ???????
- ????? ?? ?????? ?????? Tomcat ?????? ? ????????
HTTP ???????? - Tomcat ??????? ??????? HTTP ??????? ??? Internet
Explorer
HTTP/1.1 200 OK Content-Length 100 Date Fri, 26
Mar 2006 100628 GMT Server Apache-Coyote/1.1
lthtmlgtltheadgt lttitlegtHello Servletlt/titlegt lt/head
gtltbodygt lth1gtHello, Nakovlt/h1gt lt/bodygtlt/htmlgt
37Image Counter Servlet
- ?????? ?? ???????? ???????, ????? ??????? ?????
??? ??? ?? ??????????? (??????????? ? JPEG
??????) - ????????? ?????? ?? ???????? ???????? ?????
- ????? ?? ?? ?????????????? ? ?????? init()? ?? ??
??????????? ? ?????? doGet() - ?????????? ?????? ?? ? ??????????? (? JPEG
??????) - ?? ???????? ?? content type ?????? ?? ???????
"image/jpeg"
38Image Counter Servlet (2)
import javax.servlet. import javax.servlet.http.
... public class ImageCounterServlet extends
HttpServlet private String mStartDate
private int mVisitCounter public void
init() mStartDate (new
Date()).toString() mVisitCounter 0
public BufferedImage createImage(String msg)
...
39Image Counter Servlet (3)
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
IOException, ServletException String msg
synchronized(this) mVisitCounter
msg "" mVisitCounter " visits since
" mStartDate BufferedImage
image createImage(msg) response.setContent
Type("image/jpeg") OutputStream out
response.getOutputStream() // Encode the
image in JPEG format and // write the image
to the output stream
40Image Counter Servlet ? ????????
41Image Counter Servlet
????????????
42???????????? ?? ?????
43????? ? ??????
- ??????? ? ?????????, ???????? ? ?????????
??????????, ????? ?? ???????? ?? ?????? ??
??????? - ??????? ?? ???????? ????? HTTP ????????
- ??????? ?? ?????????? ?? ????????? ??????????,
????? ??????? ?? ????? ????????????? ?????.
???????? - Login / logout ??????????????
- Wizard ????????
- ??????? ?? ??????????
- ????????? ?? ????????????? ?????????
44???????????? ?? ????? ? ????????
- ?????????? ??????????? ??????? API ????????? ??
???????????? ?? ????? - ??????? ?? ????????? ???????????, ??? ?????? ??
???????????? ??? - ??? ??????????? ???????? ???????? HttpSession
????? ? ????? ??????? ?????? - ?????????? ??????? ???? ???????? ??????? ??????
?? ??????? - ???????? ?? ????? ?????? ???? ???? ? ??? ??????
????? - ??????? ????? ?? ?????????? ?????
45API ??????????? ?? ???????????? ?? ?????
- . API ??????????? ?????????
- ?????????? ?? HttpSession ????? ??
HTTPServletRequest ????? - ????????? ?? ????? ?? ???????? ????? ????????? ?
??????? - ???????? ?? ????? ??? ???????? ????? ????????? ?
??????? - ????????? ?? ???? ?????????? ?? ???????? ?????
???? ???????? ???? ? ????????? ???????
46?????????? ?? ?????? ?????
- ?? ?? ??????? ???????? ????? ?????????? ??????
HttpServletRequest.getSession() - ????????
- ??? ??????? ???? ? ?????????, ?? ?? ?????
?????????????? ????? - ??? ??????? ?? ??????????, ?? ?? ??????? ? ?????
???? ????? - ????? ?? ???????? ???? ??????? ? ???? ????
???????? ?????? isNew()
HttpSession session request.getSession()
47??? ????????
- ?????? ???????? getSession() ? ????? ??????????
??????????? ?? ??????? ???????? Session ID - ??? ???? Session ID ????? ?? ????????????
- ??????? 1 ??? ????????? ???????? ?????????,
????????? ??????????? ??????? ????????? ? ???????
session ID ? ???. - ? Tomcat, ??????????? ?? ?????? JSESSIONID
- ??????? 2 ??? ????????? ?? ???????? ?????????,
????????? ?? ????? ?? ??????? session ID ?? URL-??
48????????? ?? ????? ?? ???????
- ????????? ????? ?????? ???? HashMap
- ????????? ?????????? ?? ???????? ??? Java ??????
- ???????? ?? ?????????? ?? ???? (???? ? ???
???????) - ????????? ?? ??????????? ?????
- ????????? ?? ?????? ?? ?????? ???????
?????????? ??? ???????
Integer accessCount (Integer)session.getAttrib
ute("accessCount")
Enumeration attributes request.getAttributeNam
es()
49????????? ?? ????? ? ???????
- ????? ?? ??????? ????? ? ???????? ????? ? ?? ??
?????????? ??-????? - ????? ?? ????????? ?????? ?? ???????, ??? ??????
?? ?? ?? ?????
HttpSession session request.getSession() sessio
n.setAttribute("name", "Svetlin Nakov")
session.removeAttribute("name")
50???????????? ?????????? ?? ???????
- ?????????? ?? ?????????????? ?? ???????,
????????? ? ???????????, ????. gj9xswvw9p - ???????? ???? ??????? ? ???? ?? ?????????
- ???????? ???? ? ???? ????????? ???????
- ???????? ???? ???????? ? ???? ??????? ???????
public String getId()
public boolean isNew()
public long getCreationTime()
public long getLastAccessedTime()
51???????? ?? ?????
- ????? ?? ??????? ??????????? ???????? ??
????????? ?? ??????? (? ???????) - ??????? ??????????? ????? ?????????, ??????
??????? ????? ????????? ?? ??????? ????????? ????
???????? - ????? ?? ???????? ??????????? ???????? ??
????????? - ??????????? ???????? ??????, ?? ??????? ?????? ??
??????
public int getMaxInactiveInterval()
public void setMaxInactiveInterval (int seconds)
52???????????? ?? ?????
- ?? ?? ????????? ??????? ????? ?????????? ???????
????? - ?????????? ?? ?????? ??? ????? ?? ???????????"
- ??????? ???? ?? ????? ????????? ?? ???? ???
??????? ? ???????????? - ??????? ????? ?? ??????? ??????????? ?????? ?????
?? ?????????
public void invalidate()
53?????? Login / Logout
- ?????? ?? ???????? ?????? ??? ??????????, ?????
?????????? ??????? ???? ????? ?? ???? - ?? ?????????? ?????, ?? ?? ????????? ??????????
?? ????????? ??????????? - ?? ?????????? ????? "username"
- ??? ?? ???, ????? ???????????? ???? ? ??????
- ??? Login ?? ???????? ??????????????? ??? ?
??????? - Logout ?? ?????????? ???????
- Main servlet ?? ????????? ??????? ??????????
54Login Form
LoginForm.html
lthtmlgt ltheadgtlttitlegtLoginlt/titlegtlt/headgt
ltbodygt ltform method"POST"
action"LoginServlet"gt Please loginltbrgt
Username ltinput type"text"
name"username"gtltbrgt Password
ltinput type"password" name"password"gtltbrgt
ltinput type"submit" value"Login"gt
lt/formgt lt/bodygt lt/htmlgt
55Login Servlet
LoginServlet.java
public class LoginServlet extends HttpServlet
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException,
ServletException String username
req.getParameter("username") String password
req.getParameter("password") PrintWriter
out resp.getWriter() if (isLoginValid(usern
ame, password)) HttpSession session
req.getSession() session.setAttribute("USER
", username) response.sendRedirect("MainSer
vlet") else response.sendRedirect("
InvalidLogin.html")
56Main Servlet
MainServlet.java
public class MainServlet extends HttpServlet
public void doGet(HttpServletRequest request,
HttpServletResponse resp) throws
ServletException, IOException HttpSession
session request.getSession() String
userName (String) session.getAttribute("US
ER") if (userName ! null)
response.setContentType("text/html")
ServletOutputStream out resp.getOutputStream()
out.println("lthtmlgtltbodygtlth1gt")
out.println("Hello, " userName "! ")
out.println("lt/h1gtlt/bodygtlt/htmlgt") else
response.sendRedirect("LoginForm.html")
57Logout Servlet
LogoutServlet.java
public class LogoutServlet extends HttpServlet
protected void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException
HttpSession session request.getSession()
session.invalidate() response.setContentType(
"text/html") ServletOutputStream out
response.getOutputStream()
out.println("lthtmlgtltheadgt")
out.println("lttitlegtLogoutlt/titlegtlt/headgt")
out.println("ltbodygt") out.println("lth1gtLogout
successfull.lt/h1gt") out.println("lt/bodygtlt/ht
mlgt")
58Invalid Login Page
InvalidLogin.html
lthtmlgt ltheadgt lttitlegtErrorlt/titlegt
lt/headgt ltbodygt lth1gtInvalid login!lt/h1gt
Please lta href"LoginForm.html"gttry againlt/agt.
lt/bodygt lt/htmlgt
59???????? ? ???? ?? ????????
- ???????? ???????? ??????? ??????????? ???????? ?
??????????? - ???? ???? ?? ?????? ?? ????????? ?? ?????
?????????? ?? ?????????? - ???????? ???? ?????? ? ????????????
- ?????? ?? ???????? ??????????, ?? ?? ????????????
??????????? ?? ????? ??????????
response.setHeader("Pragma", "No-cache") response
.setDateHeader("Expires", 0) response.setHeader("
Cache-Control", "no-cache")
60Login / Logout
????????????
61????????
62??????????
- ???????? ???????, ????? ????????? ? ???????
??????? ?? 1 ?? 1000 ? ??????? ????????? ??????. - ???????? ???????, ????? ?????? ???? ????????? ???
???? ????? ? ????????? ?????? ??. - ???????? HTML ?????, ????????? ????????.
???????? ?? ?????????? ?????? GET ? POST.
63?????????? (2)
- ???????? ???????, ????? ????? ?????? ???????".
?????? ???????? ?????? ???????? ?? ???? ??? ???
???????? ?????????? ????? ? ????????? 1..100.
???????????? ?????? ?? ?????? ???? ?????. ???
????? ???? ????????? ??????? ???? ??-??????" ???
??-?????". ?????? ?????????, ?????? ????????????
?????? ???????.
64?????? ?? ???????
- ???????? ???????, ????? ?????? ???? ?????????
????? ? ?? ??????? ???? ???????????, ?????? ??
???????????? ?? OCR ???????. ????????????? ??????
?? ??? ???????? ???????? ???????. - ???????? HTML ????? ? ??????? ?? ????????????? ??
?????????? ?? ???? ????? ??????? ? ?????. ?? ??
????????? ???????? ????? ??????? ??????????,
?????, ?????????, ???, ???, ???, ????. - 1 ?? 0.01 ????? 1 ?? 1000 ?????
- 1 ??? 0.3048 ????? 1 ??? 0.0254 ?????
- 1 ??? 0.9144 ????? 1 ???? 1609.344 ?????
65?????? ?? ??????? (2)
- ???????? ???????????????? ?? HTML ????? ?
????????, ??????????? ????????? ?? ?????????? ??
???????. ???????????? ?? ??????? ?? 3 ?????? ? 3
??????? ????? - ?????? 1 ???, ???????, ???????
- ?????? 2 ????? (???????, ????, ?????)
- ?????? 3 ???????????, ????????, ???????????
- ??????? ???????? ? 3-?? ?????? ?????? ?? ??
??????? ? ??????? ? ?? ?? ??????? ??????. - ???????? ???????, ????? ???? ??????????? (??
WEB-INF\img\logo.gif) ? ?? ?????.