servlet and jsp

44
Servlet and JSP Gary 07/24/2013

Upload: gary-yeh

Post on 18-Aug-2015

70 views

Category:

Software


0 download

TRANSCRIPT

Servlet and JSP

Gary

07/24/2013

Question

• Blocking I/O如何處理多個read?

• I/O

– Blocking I/O

– Non-blocking I/O

– Asynchronous I/O

– Synchronous I/O

Question

• Blocking I/O model

recvfrom

application

System call

Process datagram

Process blocks in call to recvfrom

Wait for data

Copy data from kernel to user

No datagram ready

Datagram ready

Copy datagram

Copy completeReturn OK

kernel

Question

• Non-Blocking I/O model

recvfrom

application

System call

Process datagram

Process repeatedlycalls recvfromwaiting for an OKreturn(polling)

Wait for data

Copy data from kernel to user

No datagram ready

Datagram ready

Copy datagram

Copy completeReturn OK

kernel

EWOULDBLOCK

recvfromSystem call No datagram

readyEWOULDBLOCK

Question

• Asynchronous I/O model

recvfrom

application

System call

Signal handlerprocess datagram

Process continuesexecuting

Wait for data

Copy data from kernel to user

No datagram ready

Datagram ready

Copy datagram

Copy completeDeliver signalspecofied in recvfrom

kernel

return

Question

• Synchronous I/O

• Definition

– A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes.

Question

• How to solve multiple I/O devices?

• Use select()

A

B

A

Select()

device

Question

• 手動讓process休眠

DEFINE_WAIT(my_wqentry);初始化一個待命佇列項目void prepare_to_wait(wait_queue_head_t *queue, wait_queue_t *my_wqentry, int state);將待命佇列項目加進佇列prepare_to_wait();可以開始休眠schedule();重新加入排程void finish_wait(wait_queue_head_t *queue, wait_queue_t *my_wqentry);把柱列項目清除

Question

• Asynchronous Notification

–當新資料進來時獲得通知

–執行優先度低,無法使用輪詢

fcntl(STDIN_FILENO, F_SETOWN, getpid());讓核心知道通知對象是誰fcntl(STDIN_FILENO, FSETFL, oflags | FASYNC);啟動臨時通知

Question

• 同時存取database

– Locking

– Timestamp

– Multiversion concurrency control

Outline

• Introduction

• CGI

• Servlet

• Implement of Servlet

• JSP

• Conclusion

Introduction

• You can use JSP or servlet to solve dynamic web page needs. JSP is web page design oriented, and servlet is program designed oriented. Knowing their character can cooperate two expertise and make the achievement better.

CGI

• Common Gateway Interface

• A standard method for web server software to delegate the generation of web content to executable files.

• Web server send request to external program, and the program response with static file.

CGI

Web Server

CGI Program Database

Web Browser

1. Request

2. Input data

3. May access database 4. Response as HTML doc

Figure: CGI workflow

CGI

• CGI can designed as any computer language. Perl is the most popular.

• When server receive a request, it produce a new process to execute CGI program.

• Consume many memory.

• Each CGI program is isolated.

Servlet

• In order to solve the disadvantage of CGI, servlet is developed.

• Servlet is designed to substitute CGI.

• The word servlet is composed of “server” and “let”. In Java, “let” means small application.

• Servlet is run in server side, and applet is run in client side.

Servlet

Client Browser Web Server

Servlet Container

Servlet

1. Browser requests page

2. Web server delegates to container

3. Container executes servlet,creating instance if required

4. Servlet executes,generates response

5. Server returns response

Figure: Servlet workflow

Servlet

• Web container

– Execute JSP and servlet.

– Like Java only recognizes JVM, servlet/JSP only recognizes web container.

– Parsing HTTP request, create instance like HttpServletRequest、 HttpServletResponse、HttpSession

Servlet

• Servlet life cycle

– void init()

• When servlet is first loaded, initialize servlet status

– void service()

• Serve a request. Can be called multiple times.

– void destroy()

• Dispose servlet.

Servlet

• Since the output of servlet is HTML, the usage of servlets doesn’t constrain by browser.

• No matter how many requests, only one servlet will be loaded into JVM.

• When there is a request, there is a thread, not process.

• Servlets is persistent until be deystroyed.

Implement of Servlet

• Environment – oracle java jdk 1.7.0

• Add path to environment variables

– JAVA_HOME

– PATH

– CLASSPATH

Implement of Servlet

• Download Apache-Tomcat-7.0.42

• Use tomcat as web server and web container

Implement of Servlet

Implement of Servlet

• javax.servlet

– The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container

Implement of Servlet

• javax.servlet.http

– The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.

Implement of Servlet

• javax.servlet

Implement of Servlet

• javax.servlet.http

Implement of Servlet

• service() can handle doGet() and doPost()

• Get

– The request parameter shows on URL

– Not proper for password

– Not too long data

– Good for setting as tags or bookmarks

– Don’t change the status of server

Implement of Servlet

• Post

– The request parameter is in Body

– Good for password

– Good for long data

– Not proper for tags or bookmarks

– May change content of database

Implement of Servlet

• HttpServletRequest

– The object of client sending request to server

– E.g. Form input data

• HttpServletResponse

– The object of server responding to client

– E.g. Show HTML doc on browser

Implement of Servlet

• HttpServletResponseObjectName.setContentType(“ContentType”)

• E.g. res.serContentTyoe(“text/html;charset=Big5”)

Implement of Servlet

• In order to show information on web page, use HttpServletResponse object create a PrintWriter object.

• And use PrintWriter to write HTML tags.

Implement of Servlet

• Write web.xml

• /WEB-INF//classes/| |HelloServlet.class|web.xml

Implement of Servlet

• Web.xml

– Used to configure welcome page、 servlet、filter

– Run before load servleturl-pattern

servlet-name

servlet-class

Implement of Servlet

• The result

JSP

• HelloWorld.java

JSP

• HelloWorld.jsp

• A JSP page is basically a web page with traditional HTML and bits of Java code.

JSPFigure: JSP workflow

JSP

• When a JSP file is accessed, the container converts it into a Java class that implements the javax.servlet.jsp.HttpJspPage interface. The various JSP building blocks are translated into Java code and compiled into a Servlet.

JSP

• The element of JSP can be divided into three kinds

– Directive

• Definite the static information of web page.

– Action

• Send data according to the request of client.

– Scripting

• Small piece of java code. Only execute if client request.

JSP

directive

action

scripting

JSP

• Implicit object

• Corresponds to an object in servlet

• out– corresponds to JspWriter, which associates

PrintWriter

• Request– corresponds to HttpServletRequest

• Response– corresponds to HttpServletResponse

JSP

Conclusion

• Deal with JSP is easier than with servlet.

• Knowing how to write JSP code is helpful in developing server-side web design.