Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Tuesday, February 11, 2014

How to display all the session variables in an HTML page?

The session implicit object is an instance of javax.servlet.http.HttpSession. This variable is only valid for Http protocols. The session is one of the JSP built-in variables (like request) that is available in the service body of a JSP. 
There's no way you can do it directly from plain HTML since HTML is static and the session object really "lives" in the dynamic environment of the web container. The following example JSP shows you how to generate HTML that shows the objects in the session attribute collection.
<%@ page language="java" import="java.util.*" %>
<html>
<body>
Session attributes:
<%
  session.setAttribute("test.name", "Test Attribute List");
  session.setAttribute("test.float", new Float(5.0));
  session.setAttribute("test.int", new Integer(10));
  session.setAttribute("test.Object", new StringBuffer("StringBuffer"));
  session.setAttribute("test.boolean", new Boolean(true));
  session.setAttribute("test.double", new Double(343.1));
  for (Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) {     
    String attribName = (String) e.nextElement();
    Object attribValue = session.getAttribute(attribName);
%>
<BR><%= attribName %> - <%= attribValue %>

<%
}
%>
</body>
</html>

Original link: 
http://www.xyzws.com/JSPfaq/how-to-display-all-the-session-variables-in-an-html-page/33

Monday, February 10, 2014

How to use HttpSession




HttpSession getSession()  

          Returns the current session associated with this request, or if the request does not have a session, creates one.  
   
  
HttpSession getSession(boolean create)  
          Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.


Thursday, February 6, 2014

How to check the html button request value using JSP

Below shows you how to check the html button request value using JSP:



















JSP file:
<html>
<head>
  <title>E-bookshop</title>
  <style type="text/css">
    body {background-color:gray; font-size=10pt;}
    H1 {font-size:20pt;}
    table {background-color:white;}
    </style>
  </head>
<body>
  <H1>Your online Bookshop</H1>
  <hr/><p/>
<form name="addForm" method="POST">
      <input type="hidden" name="do_this" value="add">
      Book:
      <select name=book>
<option>Learn HTML5 and JavaScript for iOS. Scott Preston $39.99</option>
<option>Java 7 for Absolute Beginners. Jay Bryant $39.99</option>

</select>
      Quantity: <input type="text" name="qty" size="3" value="1">
      <input type="submit" value="Add to Cart">
      </form>
    <p/>

      <form name="checkoutForm"  method="POST">
        <input type="hidden" name="do_this" value="checkout">
        <input type="submit" value="Checkout">
        </form>
<br/>
<%=request.getParameter("do_this") %>
</body>
</html>

Tuesday, February 4, 2014

JSP basic


1. Directive
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.util.Date" %>

2. Declaration
In JSP, you can declare variables
in three ways:
<% int k = 0; %>
<%! int k = 0; %>
<%! static int k = 0; %>
The first declaration means that a new variable is created for each incoming HTTP client request;
the second one means that a new variable is created for each new instance of the servlet; and the third
one means that the variable is shared among all instances of the servlet.

Normally, Tomcat instantiates each one of these classes only once
and then creates a Java thread for each incoming request. It then executes the same servlet object within
each thread. If the application runs on a distributed environment or for high numbers of requests,
Tomcat can instantiate the same servlet more than once. Therefore, only the third declaration
guarantees that the variable will be shared among all requests.

3. Include another JSP on page
   <jsp:include page="index.jsp"/>

4.Scope
There are four possible scopes. Inorder of increasing generality, they are: page, request, session, and application.

5.Using an Attribute to Enable and Disable Conditional Code
One of the advantages of using JSP is that the web server doesn’t need to reinterpret the source file of a
page every time a client requests that page. The JSP container translates each JSP page into a Java file
and compiles it into a class, but this only happens when you update the JSP source. You might like to be
able to switch on or off some particular functionality for debugging or other purposes, without having to
edit one or more file and force Tomcat to recompile them when you flip the switch. To achieve this
result, you only need to wrap the functionality in question inside a conditional statement, as the

following:




































6. The out Object
The following three lines have exactly the same effect on the response:
<% out.print("abc"); %>
<%="abc"%>
abc

7.MVC
The Model 2 Architecture
A better solution, more suitable for larger applications, is to split the functionality further and use JSP
exclusively to format the HTML pages. This solution comes in the form of the JSP Model 2 architecture,
also known as the model-view-controller (MVC) design pattern (see Figure 3-2).






















Includes another JSP page in a JSP page


The following code includes another JSP page in a JSP page:
...............
<jsp:include page="index.jsp"/>
...............