JAVA Programming Language Servlet (GenericServlet) HTTP (HttpServlet) 2
(1)? CGI 3 (2) http://jakarta.apache.org JSDK(Java Servlet Development Kit) 4
(3) CGI CGI(Common Gateway Interface) /,,, Client Server URL, Data HTML with Results Parameters Results CGI Program 5 (1) JSDK startserver stopserver Jakarta startup shutdown JSWDK(JavaServer Web Development Kit) startserver stopserver 6
(2) Jakarta Jakarta-tomcat // bin jakarta tomcat. classes classpath conf jakarta tomcat doc jakarta tomcat lib jakarta src jakarta webapps jakarta work JSP 7 (3) Jakarta bin startup shutdown 8
(4) Jakarta 9 (5) (HelloServlet.java) import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public voiddoget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { res.setcontenttype("text/html;charset=ksc5601"); try { out.close(); catch(exception e) { PrintWriter out = res.getwriter(); out.println("<html>"); out.println("<head><title>hello!! Servlet!!!</TITLE></HEAD>"); out.println("<h1>hello, Servlet!!</H1>"); out.println("<h2>. </H2>"); out.println("</body>"); out.println("</html>"); getservletcontext().log("error in HelloServlet:", e); 10
(6) (HelloServlet.java) HelloServlet.java, <jakarta-tomcat>/webapps/root/web-inf/classes/ 11 (1) - (request-response). FTP, HTTP HTTP init() Servers load and run Servlets The Servlets then accept zero or more requests from clients The Servlets then return data to the clients Servers terminate servlets service() destroy() 12
(2) Servlet interface implements GenericServlet class Override one or more of... class doget( ) dopost( ) service( )... extends HttpServlet extends MyServlet class doget( ) dopost( ) service( )... 13 (3) javax.servlet java.lang.object java.io.inputstream javax.servlet.genericservlet java.io.outputstream java. io.outputstream javax.servlet.servletinputstream javax.servlet.servletoutputstream javax.servlet.servlet javax.servlet.servletconfig javax.servlet.servletcontext javax javax.servlet.servletrequest javax.servlet.servletresponse javax.servlet.servletresponse javax.servlet.requestdispatcher javax.servlet.singlethreadmodel javax.servlet.singlethreadmodel 14
(4) Servlet, HTTP 1. javax.servlet.genericservlet 2. javax.servlet.httoservlet 15 (5) Servlet void destroy( ); //. ServletConfig getservletconfig(); // ServletConfig. // ServletConfig. String getservletinfo(); //. void init(servletconfig config); //,. void service(servletrequest req, ServletResponse res); //. 16
(6), init( ), destroy( ), finalize interface Servlet { WWW Request Response init(config) service(req, res) destroy() getservletconfig () getservletinfo() Load Unload MyServlet.class 17 (7)-GenericServlet GenericServlet Servlet ServletConfig implements, ServletConfig log( ) GenericServlet, service( ) 18
(8) MyServlet extends GenericServlet { Request ServletRequest req WWW WWW ServletResponse res Response ServletInputStream ServletOutputStream service(req, res) init(config) destroy() 19 (9) ServletRequest ServletRequest 20
(10) ServletResponse MIME ServletInputStream readline 21 (11) ServletOutputStream, ServletConfig ServletContext 22
GenericServlet(1) (GenericServlet3.java) import java.io.*; import javax.servlet.*; public class HelloGenericServlet3 extends GenericServlet { public void service(servletrequest req, ServletResponse res) throwsservletexception, IOException { res.setcontenttype("text/html; charset=euc-kr"); PrintWriter out=res.getwriter(); out.println("<html>"); out.println("<head><title>hello, GenericServlet Testing </TITLE></HEAD>"); out.println("<h1>hi, Hyun Hoon</H1>"); out.println("<h2>? </H2>"); out.println("</body>"); out.println("</html>"); 23 GenericServlet(2) (GenericServlet3.java) 24
(12)-HttpServlet HttpServlet HTTP GenericServlet HttpServlet service( ) // HttpServlet doget( ); // HTTP GET dopost( ); // HTTP POST doput( ); // HTTP PUT dodelete( ); // HTTP DELETE init( ); / destroy( ); // getservletinfo( ); // 25 (13) HTTP GET Request Response POST Request WWW HttpServletRequest req Response HttpServletResponse res PUT Request Response ServletInputStream MyServlet extends HttpServlet { doget(req, res) dopost(req, res) doput(req, res) dodelete(req, res) service( ) ServletOutputStream 26
(14) HttpServletRequest HttpServlet implements HttpServletResponse HTTP,, URL, 27 HttpServlet(1) (GET / POST ) import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloHttpServlet extends HttpServlet { public void doget(httpservletrequest req, HttpServletResponse res) throws IOException, ServletException { res.setcontenttype("text/html; charset=euc-kr"); PrintWriter out = res.getwriter(); out.println("<html>"); out.println("<head>"); out.println("<title>hello</title>"); out.println("</head>"); out.println("<h1> "+toksc5601(req.getparameter("name"))+"!</h1>"); out.println("<h3> "+toksc5601(req.getparameter("job"))+".</h3>"); out.println("</body>"); out.println("</html>"); 28
HttpServlet(2) (GET / POST ) public void dopost(httpservletrequest req, HttpServletResponse res) throws IOException, ServletException { res.setcontenttype("text/html; charset=euc-kr"); PrintWriter out = res.getwriter(); out.println("<html>"); out.println("<head>"); out.println("<title>hello</title>"); out.println("</head>"); out.println("<h1> "+toksc5601(req.getparameter("name"))+"!</h1>"); out.println("<h3> "+toksc5601(req.getparameter("job"))+".</h3>"); out.println("</body>"); out.println("</html>"); public static String toksc5601(string str) throws UnsupportedEncodingException { if(str == null) { return(null); return(new String(str.getBytes("8859_1"), "KSC5601")); // 29 HttpServlet(3) (GET ) 30
HttpServlet(4) (POST ) <HTML> <HEAD> <META http-equiv="content-type" content="text/html; charset=euc-kr"> <TITLE>Form Test</TITLE> </HEAD> <BODY> <H1>POST </H1> <P> <FORM action="http://203.253.24.110:8080/ servlet/hellohttpservlet" method="post" > : <input type="text" name="name" size=16><br> : <input type="text" name="job" size=40><br> <input type="submit" value=" "> Click 31 Session and Cookies(1) Session HttpSession 32
Session and Cookies(2) HttpSession Object Part of the Java ServletAPI Name=.. address=.. phone=.. Cookies SessionID=1XQ.. Server generate HttpSession SessionID= 1XQ.. Client Order=&*@jD Cookies SessionID=1XQ.. HttpSession SessionID= 1XQ.. name=.. address=.. Cookies SessionID=1XQ.. HttpSession SessionID= 1XQ.. name=.. address=.. order=.. 33 Session and Cookies(3) Cookies. Cookies Cookies Netscape ( ) 34
Session and Cookies(4) Cookies Client Name=.. address=.. phone=.. Order=&*@jD Cookies id=.. name=.. address=.. Cookies id=.. name=.. address=.. order=.. Cookies id=.. name=.. address=.. Server generate Cookies id=.. name=.. address=.. generate Cookies order=... 35 Session and Cookies(5) Cookies import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public classloginservletextends HttpServlet{ public void dopost(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException{ res.setcontenttype("text/html;charset=euc-kr"); PrintWriter out = res.getwriter(); out.println("<html>"); out.println("<head>"); out.println("<title>login</title>"); out.println("</head>"); String user = req.getparameter("user"); user = newstring(user.getbytes("iso-8859-1"), "EUC-KR"); String pass = req.getparameter("pass"); res.addcookie(logincookie); res.addcookie(usercookie); out.println("<h1>.</h1><br>"); out.println( <A HREF=\"http://203.253.24.110:8080/servlet/StartServiceServlet\"> "); else { out.println("<h1>.</h1><br>"); out.println("<a HREF=\"/Login.html\"> "); out.println("</body>"); out.println("</html>"); out.close(); if(("casper".equals(user)) && ("casper".equals(pass))) { Cookie logincookie = newcookie("bbs -casper", "Login"); Cookie usercookie = newcookie("username", user); 36
Session and Cookies(6) Cookies import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public classstartserviceservletextends HttpServlet{ public void doget(httpservletrequest req, HttpServletResponse res) throws IOException, ServletException{ res.setcontenttype("text/html;charset=euc-kr"); PrintWriter out = res.getwriter(); out.println("<html>"); out.println("<head>"); out.println("<title>hello</title>"); out.println("</head>"); int match=0; String user = null; boolean login=false; Cookie[] cookies = req.getcookies(); if(cookies!= null) { for(int i=0;i<cookies.length;i++) { if(("bbs -casper".equals(cookies[i].getname())) && ("Login".equals(cookies[i].getValue()))) { login = true; match++; else if("username".equals(cookies[i].getname())) { user = cookies[i].getvalue(); match++; if(match >= 2) { break; out.println("<center>"); if(login) { out.println("<h2> "+user+"!</h2>"); out.println("<h3>.</h3>"); else { out.println("<h2>.</h2><br>"); out.println("<a HREF=\"/Login.html\"> "); out.println("</body>"); out.println("</html>"); public void dopost(httpservletrequest req, HttpServletResponse res) throws IOException, ServletException{ doget(req, res); 37 Session and Cookies(7) 38