Docsity

J4 Programming: Servlets, JSP, and JPA - Servlets and Form Handling, Essays (high school) of Mathematics

Partial preview of the text

Download J4 Programming: Servlets, JSP, and JPA - Servlets and Form Handling and more Essays (high school) Mathematics in PDF only on Docsity!

J4 PROGRAMMING – SERVLET/JSP & JPA

Conceive Design Implement Operate

CONTENT

Part 1: Servlets and Basic Form Handling

URL mapping with servlet

Service methods

Simple form parameter handling

Part 2: Servlets and Advanced Form Handling

Read multi-valued parameters

Understanding servlet lifecycle and multithreading

WHAT SERVLET?

With Sun MicroSystem's web programming technology,

Servlet and JSP are two important components. In there

The servlet receives requests, processes business, interacts with

the database, etc.

JSP generates interfaces (HTML, CSS, JS...) to respond to the user

From a technical point of view

Servlet is a class that inherits from HttpServlet provided by Sun

Override the service method (service(), doGet(), doPost()) to

handle the request from the user. In there

 doGet(): Handle GET request

 doPost(): Handle POST request (submit form with POST method)

 service(): Handles both types of requests (when overriding this method,

doGet() and doPost() are disabled).

EXAMPLE SERVLET

Không

phân biệt

POST hay GET

Có phân biệt^ POST và GET

EXAMPLE: ORGANIZE A SERVLET

http://.../crud/index

http://.../crud/edit/

http://.../crud/edit/TeoNV

http://.../crud/edit/...

http://.../crud/create

DEMO

Tổ chức một Servlet

SERVICE METHODS

The HttpServlet service methods have the same syntax, just

different names

 doGet(HttpServletRequest, HttpServletResponse)

 doPost(HttpServletRequest, HttpServletResponse)

 service(HttpServletRequest, HttpServletResponse)

 In there

 HttpServletRequest: is an object containing data sent from the browser

(parameters, cookies, headers) and some other system parameters (ip, server

name ...)

 HttpServletResponse: is the object containing the response data about the client

Based on the (URL, Method) pair to determine which service

method is executed (note: service() doesn't care about

Method)

INVOKE SERVICE METHOD

doPost()

method=“ post ”>

doGet()

Gõ vào ô địa chỉ của trình duyệt

<a href=“… / btec ”>Liên kết

method=“ get ”>

@WebServlet(“ / btec ”)

do Post ()

do Get ()

POST: /poly

GET: /poly

DEMO

PROCESSING FORMS

Forms are used to get data from

the user or display the data to

the user.

Normally, to work with forms, you

need to make 2 requests

GET request (link) to display the

form (call doGet())

POST request (submit form) to

process data entered into the form

(call doPost())

FORM HANDLING SERVLET ORGANIZATION

GET : http://… /login/form

POST : http://… /login/check

COMPARE POST AND GET

POST and GET are two methods of transferring data from

the browser to the server.

When submitting the form with

Method=“GET” then the form data is collected and formed into a

query string (appended after the? of the url) to be sent to the

server.

Method=“POST” then the browser will create a separate channel

to transmit data to the server

Pros and cons of POST and GET

GET is faster than POST but has some limitations like:

 Small data

 Only text allowed (cannot upload)

 Exposing sensitive data (passwords and hidden fields)

POST overcomes the limitations of GET

READ PARAMETER VALUE

Handle some special cases with

getParameter(name)

 Lấy giá trị radio được chọn

< input type=“checkbox”

name=“chk_”>

 Lấy giá trị khi có check, null khi không

check