Forms: methods and encodings
<form action="formProc.jsp" method="get">
<input name="variable" />
<input type="submit" />
</form>
<form action="formProc.jsp" method="post">
<input name="variable" />
<input type="submit" />
</form>
<form action="formProc.jsp" method="post" enctype="multipart/form-data">
<input name="variable" />
<input type="file" name="file" />
<input type="submit" />
</form>
JSP: Java code
<html>
<!-- use <% %> to insert Java code in HTML in JSP template -->
<%
String variable = request.getParameter("variable");
%>
<head>...</head>
<body>
<!--use <%= %> to print out Java variables in HTML <p><%=variable %></p>
Cookie handling
//Create cookie
Cookie cookie = new Cookie("name", "value");
cookie.setMaxAge(Integer.MAX_VALUE); //negative value for session cookie response.addCookie(cookie);
//Remove cookie from the browser
Cookie cookie = new Cookie("name", null);
cookie.setMaxAge(0);
response.addCookie(cookie);
Cookie handling
//retrieve cookie value
<%
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie: cookies) {
if ("name".equals(cookie.getName())) { %>
<%=cookie.getValue() %>
<%
Session variables
<%
//create a new object stored ON SERVER for the user identified //by a given session ID (usually maintained in the cookie)
if (session.isNew()) {
session.setAttribute("name", object);
}
%>
//retrieve the object for presentation
<p><%=session.getAttribute("name")%></p>