\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Jianmin Feng (taotao)} \pdfinfo{ /Title (jsp.pdf) /Creator (Cheatography) /Author (Jianmin Feng (taotao)) /Subject (Jsp Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{197008} \definecolor{LightBackground}{HTML}{F7FAF7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Jsp Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jianmin Feng (taotao)} via \textcolor{DarkBackground}{\uline{cheatography.com/79308/cs/19748/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Jianmin Feng (taotao) \\ \uline{cheatography.com/taotao} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 19th June, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{3.18528 cm} x{1.79172 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{J2EE structure}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{client: web browser} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} server: JSP/JServertlet & Tomcat \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Backend: database} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Eclispe & Server config \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{MVC: Struct, Spring, JSF} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.34379 cm} x{3.63321 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{JSP fundamentals}} \tn % Row 0 \SetRowColor{LightBackground} jsp \seqsplit{expression} & \textless{}\%= java statement \%\textgreater{} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} jsp scriptlet & \textless{}\% unnonymous java blocks \%\textgreater{} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} jsp import & \textless{}\%@ page import= comma separated java import \%\textgreater{} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} jsp \seqsplit{declaration} & \textless{}\%! named java blocks, ie methods \%\textgreater{} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{call java class from jsp: avoid large chuck of java code (scriptlets or declarations) in jsp} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{\textless{}jsp:include file="header.jsp" /\textgreater{}} \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{jsp built-in objects}}} \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} request & http request header + form data \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} response & Http support for send reponse \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} out & jspWriter: include content in html \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} session & unique session for each user across different page \tn % Row Count 19 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{application} & shared among users of web app \tn % Row Count 21 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Read html form in JSP}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textless{}form id="test" action ="student-response.jsp" method='post"\textgreater{} \newline name: \textless{}input type="text" name="name"/\textgreater{} \newline \newline \textless{}input type="radio" name="isSophormore" value="Yes'\textgreater{} Yes \newline \textless{}input type="radio" name="isSophormore" value="No'\textgreater{} No \newline \newline \textless{}input type="checkbox" name="language" value="Javascript"\textgreater{} Javascript \newline \textless{}input type="checkbox" name="language" value="Java"\textgreater{} Java \newline \textless{}input type="checkbox" name="language" value="SQL"\textgreater{} SQL \newline \textless{}input type="checkbox" name="language" value="python"\textgreater{} Python \newline \newline \textless{}input type="submit" value="Submit"/\textgreater{} \newline \textless{}/form\textgreater{} \newline \newline \textless{}select name="country" form="test"\textgreater{} \newline \textless{}option value="China"\textgreater{} China \textless{}/option\textgreater{} \newline \textless{}option value="US"\textgreater{} US \textless{}/option\textgreater{} \newline \textless{}option value="Other"\textgreater{} Other \textless{}/option\textgreater{} \newline \textless{}/select\textgreater{} \newline \newline in JSP \newline \$\{param.name\} \$\{param.country\} \newline \textless{}\%=request.getParameter("country")\%\textgreater{} \newline \newline \textless{}ul\textgreater{} \newline \textless{}\% \newline String {[}{]} \seqsplit{language=request.getParameterValues("language");} \newline if (language !=null) \newline for (String s:language) \newline out.println("\textless{}li\textgreater{}"+s+"\textless{}/li\textgreater{}"); \newline \%\textgreater{} \newline \textless{}/ul\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Session -Cookies}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Cookie api : javax.servlet.http; \newline imported for all jsp implicitly \newline only send to specific server domain matched \newline \newline \textless{}!-{}-send cookies to web browser -{}-\textgreater{} \newline \textless{}\% \newline String \seqsplit{favLang=request.getParameter("FavorateLanguage");} \newline Cookie the Cookie=new Cookie ("myApp.favorateLang",favLang); \newline theCookie.setMaxAge(606024*365); //default 30min \newline response.addCookie(theCookie); \newline \%\textgreater{} \newline \newline cookies and session: \newline \newline 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. \newline \newline 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. \newline \newline \textless{}!-{}-read cookies in web browser-{}-\textgreater{} \newline \textless{}\% \newline Cookie{[}{]} theCookies =request.getCookies(); \newline if (theCookies !=null) \{ \newline for (Cookie tempCookie:theCookies)\{ \newline if \seqsplit{("myApp.favorateLang".equals(tempCookie}.getName()))\{ \newline \seqsplit{favLang=tempCookie.getValue();} \newline break; \newline \} \newline \} \newline \} \newline \%\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Session (copy)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{unique for a user, share data across pages,like a shopping cart for a user} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Kept in memory, each user has a session id} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{session id (create, transfer ) is handled by browse automatically} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{seesion.setAttribute("name"}, value) & \seqsplit{setAttribute(String}, Object) \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} (List\textless{}String\textgreater{} ) \seqsplit{seesion.getAttrribute("name")} & Object \seqsplit{getAttribute(String)} \tn % Row Count 10 (+ 3) % Row 5 \SetRowColor{white} session.isNew() & session.getId() \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} session.invalidate() & \seqsplit{session.setMaxInactiveInterval(ms)} \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{1. create form 2 check set session attribute 3 read from session} \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{to disable session: \%@ page session="false" \%\textgreater{}} \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{PageContext} \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Cookie API in javax.servlet.http, imported implicitly for all jsp pages} \tn % Row Count 19 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Cookie(String name, String value);} \tn % Row Count 20 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Cookie the Cookie=new Cookie ("myApp.favorateLang",favLang)} \tn % Row Count 22 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{theCookie.setMaxAge(60{\emph{60}}24*365); //default 30min} \tn % Row Count 23 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{response.addCookie(theCookie);}} \tn % Row Count 24 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Cookie{[}{]} theCookies =request.getCookies();} \tn % Row Count 25 (+ 1) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{if (theCookies !=null) \{ for (Cookie tempCookie:} \tn % Row Count 26 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{JDBC}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{install mysql, work bench + shell + demo data setup \newline \newline setup tomcat 7.0: download tomcat 7, unzip to c:\textbackslash{} \newline \newline setup connection pool: multiple users ( amazon) using the same db, need more connections, pool together to save resources ( not setup connection every time, preconfigured connect in the pool) \newline \newline download jdbc mysql connector jar -\textgreater{} WebContent/WEB-INF/lib \newline define connection pool: \seqsplit{WebContent/META-INF/context}.xml \newline \newline \textless{}Context\textgreater{} \newline \textless{}Resource \seqsplit{name="jdbc/web\_student\_tracker"} \newline auth="Container" \seqsplit{type="javax.sql.DataSource"} \newline maxActive="20" maxIdle="5" maxWait="10000" \newline username="webstudent" password="webstudent" \newline \seqsplit{driverClassName="com.mysql.jdbc.Driver"} \newline \seqsplit{url="jdbc:mysql://localhost:3306/web\_student\_tracker}?useSSL=false"/\textgreater{} \newline \textless{}/Context\textgreater{} \newline \newline resource injection: tomcat automatically set the connection pool/ datasource on your servlet. \newline \newline -{}-connector/J 8 \newline alter user 'webstudent'@'localhost' identified with mysql\_native\_password by 'webstudent';} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{cache busting?}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\seqsplit{https://curtistimson.co.uk/post/front-end-dev/what-is-cache-busting/}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}