Show Menu

Jsp Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

J2EE structure

client: web browser
server: JSP/JS­erv­ertlet
Backend: database
Server config
MVC: Struct, Spring, JSF

JSP fundam­entals

jsp expression
<%= java statement %>
jsp scriptlet
<% unnonymous java blocks %>
jsp import
<%@ page import= comma separated java import %>
jsp declar­ation
<%! named java blocks, ie methods %>
call java class from jsp: avoid large chuck of java code (scrip­tlets or declar­ations) in jsp
<js­p:i­nclude file="h­ead­er.j­sp­" />
jsp built-in objects
http request header + form data
Http support for send reponse
jspWriter: include content in html
unique session for each user across different page
shared among users of web app

Read html form in JSP

<form id="test" action ="student-response.jsp" method='post">
  name: <input type="text" name="name"/>

  <input type="radio" name="isSophormore" value="Yes'> Yes
  <input type="radio" name="isSophormore" value="No'> No

  <input type="checkbox" name="language" value="Javascript"> Javascript
  <input type="checkbox" name="language" value="Java"> Java
  <input type="checkbox" name="language" value="SQL"> SQL
  <input type="checkbox" name="language" value="python"> Python

  <input type="submit" value="Submit"/>

<select name="country" form="test">
   <option value="China"> China </option>
   <option value="US"> US </option>
   <option value="Other"> Other </option>

in JSP
${} ${}

    String [] language=request.getParameterValues("language");
    if (language !=null)
       for (String s:language)

Session -Cookies

Cookie api : javax.servlet.http; 
imported for all jsp implicitly
only send to specific server domain matched

<!--send cookies to web browser -->
String favLang=request.getParameter("FavorateLanguage");
Cookie the Cookie=new Cookie ("my­App.fa­vor­ate­Lan­g",f­avLang);
theCoo­­tMa­xAg­e(6­06­02­4*365); //default 30min

cookies and session:

A cookie is a bit of data stored by the browser and sent to the server with every request. Cookies with no expiration time will be deleted after browser closed ( some browser).facility the session.

A session is a collection of data stored on the server and associated with a given user (usually via a cookie containing an id code); session deleted at reconnection to server, give http a state.

<!--read cookies in web browser-->
Cookie[] theCookies =reque­st.g­et­Coo­kies();
if (theCo­okies !=null) { 
  for (Cookie tempCo­okie:theCookies){
    if ("my­App.fa­vor­ate­Lan­g".equals(tempCookie.getName())){

Session (copy)

unique for a user, share data across pages,like a shopping cart for a user
Kept in memory, each user has a session id
session id (create, transfer ) is handled by browse automa­tically
seesio­n.s­etA­ttr­ibu­te(­"­nam­e", value)
setAtt­rib­ute­(St­ring, Object)
(List<­Str­ing> ) seesio­n.g­etA­ttr­rib­ute­("na­me")
Object getAtt­rib­ute­(St­ring)
1. create form 2 check set session attribute 3 read from session
to disable session: %@ page sessio­n="f­als­e" %>
Cookie API in javax.s­er­vle­t.http, imported implicitly for all jsp pages
Cookie­(String name, String value);
Cookie the Cookie=new Cookie ("my­App.fa­vor­ate­Lan­g",f­avLang)
theCoo­­tMa­xAge(606024*365); //default 30min
Cookie[] theCookies =reque­st.g­et­Coo­kies();
if (theCo­okies !=null) { for (Cookie tempCo­okie:


install mysql, work bench + shell + demo data setup

setup tomcat 7.0: download tomcat 7, unzip to c:\

setup connection pool: multiple users ( amazon) using the same db, need more connec­tions, pool together to save resources ( not setup connection every time, precon­figured connect in the pool)

download jdbc mysql connector jar -> WebContent/WEB-INF/lib
define connection pool: WebContent/META-INF/context.xml

  <Resource name="jdbc/web_student_tracker" 
    auth="Container" type="javax.sql.DataSource"
    maxActive="20" maxIdle="5" maxWait="10000"
    username="webstudent" password="webstudent" 

resource injection: tomcat automatically set the connection pool/ datasource on your servlet.

--connector/J 8
alter user 'webstudent'@'localhost' identified with mysql_native_password by 'webstudent';

cache busting?