Wednesday, 11 March 2015

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" />















Wednesday, 11 February 2015

Generating output in html or in excel format based on user input

1) HTML Input File :


<html>
<body>

<form  action="readdata" method="get" >
<fieldset>

<br/>
<b> Do you want Request Header Data? ( in Html or Excel)" : </b>
<input type="radio" name="doctype" value="html" checked /> Html
<input type="radio" name="doctype" value="excel" /> Excel
<br/>
<input type="submit" />
</fieldset>
</form>

</body>


2) Servlet that generate dynamic content (in html or in excel format ) based on user input :

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

public class ServletReadData extends HttpServlet {

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

  {
   
String sdoctype = request.getParameter("doctype");
if (sdoctype.equals("html")) {
response.setContentType("text/html"); }
else if (sdoctype.equals("excel") )
{ response.setContentType("application/vnd.ms-excel"); }
   


PrintWriter out = response.getWriter();
out.println("<html> <head> <title> First Servlet ... </title></head>");
out.println("<body> <h1> Java Servlet Request Header Reading.. </h1>");


       Enumeration myheader =request.getHeaderNames();

out.println( "<table border='2'> <tr> <th> Header Name </th><th> Header Values </th> </tr>");

while (myheader.hasMoreElements() )
{
String myhname =(String) myheader.nextElement();
//Header Name
out.println("<tr> <td>");
out.println( myhname);
out.println("</td>");
//values of header
Enumeration myhvalue = request.getHeaders(myhname);
out.println("<td>");
while (myhvalue.hasMoreElements() )
{
out.println(myhvalue.nextElement());
}
out.println("</td>");
out.println("</tr>");

}

out.println("</body></html>" );

  }  //end of doPost method

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

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


}

Tuesday, 10 February 2015

Servlet Reading HTTP Request Headers and values associated with each request header

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

public class ServletReadData 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> Java Servlet Request Header Reading.. </h1>");

Enumeration myheader =request.getHeaderNames();

out.println( "<table border='2'> <tr> <th> Header Name </th><th> Header Values </th> </tr>");
while (myheader.hasMoreElements() )
{
String myhname =(String) myheader.nextElement();
//Header Name
out.println("<tr> <td>");
out.println( myhname);
out.println("</td>");
//values of header
Enumeration myhvalue = request.getHeaders(myhname);
out.println("<td>");
while (myhvalue.hasMoreElements() )
{
out.println(myhvalue.nextElement());
}
out.println("</td>");
out.println("</tr>");

}

out.println("</body></html>" );

  }  //end of doPost method

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

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


} // end of class

_______________________________________________________________________________

The output of the Above Servlet Program:


Java Servlet Request Header Reading..

Header Name
Header Values
host
localhost:8080
connection
keep-alive
content-length
53
cache-control
max-age=0
accept
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
origin
http://localhost:8080
user-agent
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
content-type
application/x-www-form-urlencoded
referer
http://localhost:8080/awtad2015/callservlet.html
accept-encoding
gzip, deflate
accept-language
en-US,en;q=0.8