Chapter 12. JSP, MVC
ITSS Java Programming NGUYEN Hong Quang, HUT
Content
Principle of JSP Elements of JSP JavaBeans MVC architecture
Principle of JSP
Outline of JSP
JSP Page : embedded Java code into HTML
page
Principle of JSP
Mecanism of calling JSP page
JSP Compiler
Server
Temporary Servlet
JSP Page Web Browser
Principle of JSP
JSP Servlet (Temporary Servlet)
public final class HelloWorld_jsp extends
org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent {
public void _jspInit() { … }
public void _jspDestroy() { … }
public void _jspService (HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { … }
}
Principle of JSP
Life cycle of JSP Servlet
Principle of JSP
Sample application (1)
Principle of JSP
Sample application (2)
Principle of JSP
Start of JSP (Calling a JSP page)
Direct start from Web browser:
input directly URL of JSP page into Web browser http://servername:8080/JSP/HelloWorld.jsp
Start by “Action” attribute of Form tag
Start when you push the summitting button on
form
MvcServlet.java (1)
public class MvcServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String name = req.getParameter( "name"); String password = req.getParameter("password"); System.out.println(name + " " + password); Customer cust = new Customer(); cust.setName(name); cust.setPassword(password); req.setAttribute("customer", cust);
MvcServlet.java (2)
String page = null; if (cust.isValid()) {
page = "/valid.jsp";
} else {
page = "/invalid.jsp";
} try {
getServletContext().getRequestDispatcher(page).forward(re q, res);
} catch (Exception ex) { ex.printStackTrace();
}
Customer.java (1)
package bean; import java.io.Serializable; public class Customer implements Serializable {
private String name; private String password; private String validName = "HUT Student“; private String validPassword = "hut“; public Customer() {} public void setName(String name) {
this.name = name;
}
Customer.java (2)
public String getName() { return name; } public void setPassword(String password) {
this.password = password;
} public String getPassword() {
return password;
} public boolean isValid() {
if (!name.equals("") && name.equals(validName) &&
password.equals(validPassword)) { return true;
} else {return false; }
}
valid.jsp
<%@page contentType="text/html; charset=utf8" %>
scope="request" />Welcome
Mr./Ms. :
invalid.jsp
<%@page contentType="text/html; charset=utf8" %>
The user name or the password is illegal.
Handons exercices
JSP : input personal information and then
display to the client
JavaBean :
SimpleCalculator.java add/substract/multiply/divide two numbers
Handons exercices
MVC :
valid.jsp (View) invalid.jsp (View) SimpleCalculator.java (Model) calculator.htm (View) ControllerServlet :
if two input number aren’t on good format, forward to
If not : call SimpleCalculator and display the result by
invalid.jsp
valid.jsp