git clone https://github.com/mhsu0020/CSULA-CS3220-Fall2016.git
git pull
The past two weeks we focued on client side development. The next couple of weeks we will focus on server side development (you will still use client side concepts.)
init()
: configuring on application startuprequest.setAttribute
Common components of an dynamic web application include controllers, for controlling routing of user requests and responses; models, for data modeling (think java data objects); and views, usually some kind of templating language that allows generation of HTML code using model
http://localhost:8080/HelloExample/HelloWorld
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//Handles the resource path HelloWorld
@WebServlet( "/HelloWorld" )
public class HelloWorld extends HttpServlet {
public void doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletExceptoin, IOException
{
PrintWriter out = response.getWriter();
out.println( "Hello World" );
}
}
Some observations:
HTML example from lecture 3
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Setting the Content-Type Header
response.setHeader("Content-Type", "text/html");
response.getWriter().append(
"<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>CS3220 Rocks!</title></head><body><h1>CS3220 is Awesome!!!</h1><p>This class is lit</p></body>");
}
}
@WebServlet(URL_Pattern)
Request URL: http://host/app/path
loadOnStartup
), specify the urlPatterns propertyJust value
:@WebServlet("/HelloWorld")
Mapping to multiple patterns:@WebServlet(urlPatterns={"/HelloWorld", "/Example", "/hahahaha"})
Additional Props:@WebServlet(urlPatterns="/HelloWorld", loadOnStartup=1)
init()
:super.init(config)
first. Very handy for setting up shared variables between servletsservice()
:destroy()
: When the Servlet is unloadedFrist step: retrieve the ServletContext
getServletContext()
: returns the HttpServletContext object
useful HttpServletContext
instance methods:
setAttribute(String name, Object value)
:getAttribute(String name)
:loadOnStartup
loadOnStarup
element of @WebServlet
to have it created during application startuploadOnStartup
is the order which the application will start the servlets@WebServlet(urlPatterns="/HelloWorld", loadOnStartup=1)
init()
Example:
@Override
public void init( ServletConfig config ) throws ServletException
{
// When you override init(), don't forget to call the super class
// init() first.
super.init( config );
// Note that Java automatically converts between int and Integer
// (i.e. the "Autoboxing and Unboxing" feature since JDK 1.5.
int counter = 0;
getServletContext().setAttribute( "counter", counter );
}
Full Example: Shared counter between Servlets, and Quizzes
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Hello JSP 1</TITLE></HEAD>
<BODY>A JSP without J or S.</BODY>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>JSP Hello World</TITLE></HEAD>
<BODY>Hello World on <%= new java.util.Date() %>.
</BODY>
</HTML>
<%@ type attr=“value” ... %>
<%-- Hidden Comments --%>
and <!-- Output (HTML) Comments -->
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><head><title>JSTL Hello</title></head>
<body>
<c:out value="Hello World in JSTL." />
</body>
</html>
URI
Prefix
Library | URI | Prefix |
---|---|---|
Core | http://java.sun.com/jsp/jstl/core | c |
XML Processing | http://java.sun.com/jsp/jstl/xml | xml |
I18N Formatting | http://java.sun.com/jsp/jstl/fmt | fmt |
Database Access | http://java.sun.com/jsp/jstl/sql | sql |
Functions | http://java.sun.com/jsp/jstl/functions | fn |
Docks: http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/index.html
Flow control
URL
Output
Expression Language (EL)
${ expression }
${bean_name.property_name}
<%-- single if --%>
No Quizzes.
<%-- if else branching --%>
No quizzes yet.
<%-- display the quizzes --%>
Code example: Quiz.jsp
<%-- The varStatus property gives information about the current loop.--%>
<%-- remember loop.index calls getIndex --%>
${quiz.questionText}
Code example:Quizzes.jsp
see http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/jstl/core/LoopTagStatus.html
<fmt:formatDate value="${date}" type="date" />
<fmt:formatDate value="${date}" type="time" />
<fmt:formatDate value="${date}" type="both" />
<fmt:formatDate value="${date}" pattern="yyyy-MM-dd hh:mm:ss a" />
Code example: Controller and View
see http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Model
View
Controller
out.println()
request.getRequestDispatcher( "/WEB-INF/myJsp.jsp" )
.forward( request, response );
request.setAttribute( "objName", obj );
request.getRequestDispatcher( "/WEB-INF/yourJSP.jsp" )
.forward( request, response );