Wednesday, October 21, 2009

JSP - gather, process, store, and use session variables

<!-- 
just a typical html form 
This is the "theform.jsp" page 
--> 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head><title>enter form data</title></head> 
    <body> 
    <form method="post" action="formSaver.jsp"> 
        Username: <input type="text" name="username" /><br /> 
        Cool Factor: <input type="text" name="coolfactor" /><br /> 
        <input type="submit" value="submit" /> 
    </form> 
    </body> 
</html> 
 
 
<!-- 
    this page grabs the form data and 
    and (with the help of the UserFormBean class) 
    places the items as session data. 
--> 
 
<!-- the useBean taq is where this jsp page loops in the real classes --> 
<jsp:useBean id="userform" class="userform.UserFormBean" scope="session"/>
<!-- the setProperty tag sends * (all) the property data to userform --> 
<jsp:setProperty name="userform" property="*"/>
<html> 
    <head><title>Form Saver</title></head> 
    <body> 
        <p>Your data has been placed in a session!</p> 
        <p>Try visiting this <a href="unrelatedPage.jsp">unrelated page!</a></p> 
    </body> 
</html> 
 
 
<!-- this is the unrelated page that will pull 
    and display the session data --> 
<!-- this loops in the session data we'll utilize --> 
<jsp:useBean id="userform" class="userform.UserFormBean" scope="session"/>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head><title>an unrelated page</title></head> 
    <body> 
       <%= userform.getUsername() %> | <%= userform.getCoolfactor() %> 
    </body> 
</html> 

/* 
 * This is the actual java class "UserFormBean" 
 * that processes and stores the data. 
 * 
 * Notice that the set methods use 
 * the same names as the form name data! 
 * 
 * */ 
package userform;
 
public class UserFormBean {
    String username;
    String coolfactor;
 
    public void setUsername(String value) {
        username = value;
    }
    public void setCoolfactor(String value) {
        coolfactor = value;
    }
    public String getUsername() {
        return username;
    }
    public String getCoolfactor() {
        return coolfactor;
    }
}
 
 

Tuesday, October 20, 2009

JSP - list all parameter keys and values

<%@page import="java.util.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP display all parameters</title> 
    </head> 
    <body> 
        <% 
 
            // you can get an enumeratable list 
            // of parameter keys by using request.getParameterNames() 
            Enumeration en = request.getParameterNames();
 
            // enumerate through the keys and extract the values 
            // from the keys! 
            while (en.hasMoreElements()) {
                String parameterName = (String) en.nextElement();
                String parameterValue = request.getParameter(parameterName);
                out.println(parameterName+":"+parameterValue+"<br />");
            }
 
            // now call your jsp file (from a browser and add on some paramters) 
            // file.jsp?a=12341234&b=apple&c=1.21gigawatts 
 
        %> 
    </body> 
</html> 
<!-- 
output: 
    a:12341234 
    b:apple 
    c:1.21gigawatts 
 
--> 
 

JSP - include several differnt files to create content

<!-- This is the file "head.jsp" --> 
<!-- This file just has head type info --> 
<html> 
    <head> 
        <title>the title</title> 
    </head> 
 
 
<!-- this is the file "body.jsp" --> 
<!-- this file contains the content portion of the site --> 
 
<body> 
    <p> 
        Content goes here
    </p> 
</body> 
</html> 
 
<!-- this is the file called main.jsp --> 
<!-- this files purpose is to bring in all the 
 
<%@ include file="head.jsp" %>
<%@ include file="body.jsp" %>
 
    <% 
 
    // When you include files you bring the file's data 
    // (whether its html or jsp code) into the calling 
    // page. 
 
    %> 
 

JSP - use directives to import java libraries

<%@ page import="java.util.*" %>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP Page</title> 
    </head> 
    <body> 
 
        <% 
        ArrayList ll = new ArrayList();
 
            for (int i=0; i<100; i++) {
                ll.add(i);
            }
        
        %> 
 
        <h1>ArrayList size: <%= ll.size() %> </h1> 
        <h1>Location of item 77: <%= ll.indexOf(77) %> </h1> 
 
    </body> 
</html> 
 
 

JSP - embedding scriptlets inside and around html

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP Page</title> 
    </head> 
    <body> 
        <% 
        // if you need to accomplish a repetitive task 
        // like creating a list of 1,000 numbers and 
        // putting them in a bulletted list then 
        // keep in mind that scriptlets can be your 
        // friend. 
 
        // You notice below that the for loop spans several 
        // pieces of the scriptlet 
        %> 
        <ul> 
        <% 
        for (int i=0; i<1000; i++) {
        %> 
            <li><%= i %></li> 
        <% 
        }
 
        %> 
        </ul> 
        
    </body> 
</html> 
<!-- 
0 
1 
2 
3 
... 
[SNIP] 
... 
998 
999 
--> 
 
 

JSP - declare methods and variables from within a jsp page

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP Page</title> 
    </head> 
    <body> 
        <%! 
            // You can do real coding inside of here! 
            // The code in this section is called a scriptlet. 
            // Any functions or variables you create can 
            // be used later in the file with jsp expression 
            // tags or shortcuts 
 
            // You'll notice that the section began with a 
            // <%! ......this allows you to declare methods 
 
            String s = "Declare methods and variables with scriptlets.";
 
            int getNum() {
                if (1>0){
                    return 100;
                }
                return 111;
            }
        %> 
 
        <h1><%= s %></h1> 
        <h2><%= getNum() %></h2> 
 
    </body> 
</html> 
 
 

JSP - print direct to html

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP print straight out example</title> 
    </head> 
    <body> 
        <% 
            out.println("<h1>Print directly example</h1>");
            out.println("Here is the date:<br />");
            out.println(new java.util.Date());
        %> 
    </body> 
</html> 
<!-- 
my output: 
    Print directly example 
 
    Here is the date: 
    Tue Oct 20 11:21:21 MDT 2009 
-->