Monday, 30 March 2015

Accessing ArrayList , HashMap attached to Servlet using JSP Expression Language and JSTL Tag Library

/ **
Servlet class in which ArrayList and HashMap is created and attached to ServletContext Object

*/

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class ServletExpression extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
    response.setContentType("text/html");

PrintWriter out = response.getWriter();
out.println("<html> <head> <title> First Servlet ... </title></head>");
out.println("<body> <h1> Welcome to JAVA  </h1>");

ArrayList<String> alist = new ArrayList<String>();

alist.add("ISTAR");  alist.add("ADIT");
alist.add("VPSCI");  alist.add("NVPAS");

HttpSession session = request.getSession();
session.setAttribute("instname" ,alist);


//Setting hashmap in Application

HashMap acnt = new HashMap();

acnt.put("abc",200000);
acnt.put("xyz",100000);
acnt.put("pqr",300000);
acnt.put("lmn",400000);

ServletContext application = request.getServletContext();

application.setAttribute("acctname",acnt);

out.println("</body></html>" );
 
  }  //end of method
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
doPost(request, response);
  }  //end of doGet method


}




2) Accessing ArrayList and HashMap elements in JSP Page (displayApplication.jsp):


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:out value="JSTL Tag Example" />

<hr/>
1:
${instname[0]} <br/>
2:
${instname[1]} <br/>
3:
${instname[2]} <br/>
4: <c:out value="${instname[3]}" default="ISTAR" /> <br/>
5: <c:out value="${instname[4]}" default="ISTAR" /> <br/>
6: <c:out value="${instname[5]}" default="ISTAR" /> <br/>


<hr/>
<b>Using JSTL , accessing ArrayList values::: </b><br/><br/>

<c:forEach var="i" items="${instname}" >

${i} <br/>
</c:forEach>

 <hr/>
 <b>
 <c:out value="<c:forEach> examples ::" /> </b>
 <br/>
 <c:forEach var="i" begin="1" end="10" >

${i}
<c:if test="${i<3}"> less than 3 </c:if>
<c:if test="${i<6}"> less than 6 </c:if>
<c:if test="${i<8}"> less than 8 </c:if>
<br/>
 </c:forEach>

 <hr/> <br/>

 <b>
 <c:out value="<c:choose> example:::" /> </b><br/>

 <c:forEach var="i" begin="1" end="10" >

${i}
<c:choose>
<c:when test="${i<3}"> less than 3 </c:when>
<c:when test="${i<6}"> less than 6 </c:when>
<c:when test="${i<8}"> less than 8 </c:when>
<c:when test="${i==8}"> equal to 8 </c:when>
<c:otherwise> greater than 8 </c:otherwise>
</c:choose>
<br/>
</c:forEach>

 <hr/>

 <b> HashMap example :::</b>
 <br/>


<c:forEach var="i" items="${acctname}" >

${i.key} has account balance US \$ ${i.value} <br/>

</c:forEach>

 <hr />
 <b>
 Reverse Order print : </b>
 <br/>

 <c:forEach var="i" begin="1" end="10"  step="2">
 <c:set var="revorder" value="${11-i}" />
${revorder} <br/>
 </c:forEach>

 <hr/>

Wednesday, 11 March 2015

Creating Attribute at Request ,Session and Application in Servlet class

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import education.Student;

public class SessionApplicationDemo extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
    response.setContentType("text/html");

PrintWriter out = response.getWriter();
out.println("<html> <head> <title> First Servlet ... </title></head>");
out.println("<body> <h1> Welcome to JAVA  </h1>");
out.println("Hello World <br>" );

Student student1 = new Student();
student1.setSid(2020);
student1.setSname("Hitesh");

Student student2 = new Student();
student2.setSid(3020);
student2.setSname("Nayan");
//access ServletContext

ServletContext application = request.getServletContext();

application.setAttribute("s2",student1);
//accessing application attribute
Student st1 = (Student) application.getAttribute("s2");

out.println(st1.getSid() +"<br/>");
out.println(st1.getSname());



//creating session

HttpSession session = request.getSession();

session.setAttribute("s3",student2);

/*request.setAttribute("s2",student1);
//accessing request attribute
Student st1 = (Student) request.getAttribute("s2");

out.println(st1.getSid() +"<br/>");
out.println(st1.getSname());
*/
//access session attribute
Student st2 = (Student) session.getAttribute("s3");

out.println(st2.getSid() +"<br/>");
out.println(st2.getSname());

out.println("</body></html>" );
 
  }  //end of method
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

  {
doPost(request, response);
  }


}

Java Bean Example

//Student Bean class

package education;

public class Student {
private int sid;
private String sname;

public void setSid(int sid) {
this.sid = sid;
}
public void setSname(String sname) {
this.sname = sname;
}

public int getSid() { return sid; }

public String getSname() { return sname; }

}

_________________________________________________________________________________

JSP Pages 
1) inputform.jsp
 ________________________________________________________________________________

<html>

<body>
<form action="master.jsp" >
SID :
<input type="text" name="sidtest" /><br>
SNAME:
<input type="text" name="snametest" /> <br>
<input type="Submit" />

</form>
</body>

</html>
_________________________________________________________________________________
2 )  master.jsp
_________________________________________________________________________________

<html>


<body>

<hr/>

Main Page<br/>

<hr/>
Student Information :
<br/>
Using JavaBeans
<br/>
Student data:

<%--
here scope="session" or scope="request" or scope="application"
based on requirement where Student object is to be attached as attribute
 --%>


<jsp:useBean id="st1" class="education.Student" scope="application" />


<jsp:setProperty name="st1" property="sid" param="sidtest" />

<jsp:setProperty name="st1" property="sname" param="snametest"/>

ID:
<jsp:getProperty name="st1" property="sid" />
<br/>
NAME:
<jsp:getProperty name="st1" property="sname" />



<hr/>
<jsp:include page="WEB-INF/jsp/displayrequest.jsp" />

</body>

</html>
________________________________________________________________________________

3. ) displayrequest.jsp
________________________________________________________________________________

<hr/>

Included page
<br/>
<br/>
<jsp:useBean id="st1" class="education.Student" scope="request" />


SID:<jsp:getProperty name="st1" property="sid" />
<br/>
NAME:
<jsp:getProperty name="st1" property="sname" />

________________________________________________________________________________
4. ) displaysession.jsp
________________________________________________________________________________

<hr/>
Session Page
<br/>
<jsp:useBean id="st1" class="education.Student" scope="session" />


SID:<jsp:getProperty name="st1" property="sid" />
<br/>
NAME:
<jsp:getProperty name="st1" property="sname" />

________________________________________________________________________________
5. ) displayapplication.jsp
________________________________________________________________________________

<hr/>
Application Page

<br/>

<jsp:useBean id="st1" class="education.Student" scope="application" />


SID:<jsp:getProperty name="st1" property="sid" />
<br/>
NAME:
<jsp:getProperty name="st1" property="sname" />