Show Menu
Cheatography

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
Tomcat
Backend: database
Eclispe
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
request
http request header + form data
response
Http support for send reponse
out
jspWriter: include content in html
session
unique session for each user across different page
applic­ation
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"/>
</form>

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

in JSP
${param.name} ${param.country}
<%=request.getParameter("country")%>

<ul>
<%
    String [] language=request.getParameterValues("language");
    if (language !=null)
       for (String s:language)
          out.println("<li>"+s+"</li>");
%>
</ul>
 

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­kie.se­tMa­xAg­e(6­06­02­4*365); //default 30min
respon­se.a­dd­Coo­kie­(th­eCo­okie);
%>

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())){
       favLang=tempCookie.getValue();
       break;
    }
  }
}
%>

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)
sessio­n.i­sNew()
sessio­n.g­etId()
sessio­n.i­nva­lid­ate()
sessio­n.s­etM­axI­nac­tiv­eIn­ter­val(ms)
1. create form 2 check set session attribute 3 read from session
to disable session: %@ page sessio­n="f­als­e" %>
PageCo­ntext
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­kie.se­tMa­xAge(606024*365); //default 30min
respon­se.a­dd­Coo­kie­(th­eCo­okie);
Cookie[] theCookies =reque­st.g­et­Coo­kies();
if (theCo­okies !=null) { for (Cookie tempCo­okie:
 

JDBC

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

<Context>
  <Resource name="jdbc/web_student_tracker" 
    auth="Container" type="javax.sql.DataSource"
    maxActive="20" maxIdle="5" maxWait="10000"
    username="webstudent" password="webstudent" 
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/web_student_tracker?useSSL=false"/>
</Context>

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?