Friday, August 3, 2012

java simple programs


1.SAMPLE SERVLET
Sample.java

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

public class Sample extends GenericServlet
{
  public void service(ServletRequest request,ServletResponse response)throws IOException,ServletException
  {
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    out.println("Hai I am sample Servlet");
  }
}

web.xml
<web-app>
  <servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>Sample</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>sample</servlet-name>
   <url-pattern>/sample</url-pattern>
 </servlet-mapping>
</web-app>

2. SESSION TRACKING

Session1.java

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

public class Session1 extends HttpServlet
{
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
  {
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    HttpSession session=request.getSession(true);
    out.println("Session ID :"+session.getId()+"<br>");
    out.println("Creation Time: "+session.getCreationTime()+"<br>");
    out.println("Last Access Time: "+session.getLastAccessedTime()+"<br>");
    out.println("Servlet Context: "+session.getServletContext()+"<br>");
   
    if(session.isNew())
       out.println("New Session");
    else
       out.println("Old Session");
  }
}

web.xml

<web-app>
  <servlet>
    <servlet-name>session</servlet-name>
    <servlet-class>Session1</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>session</servlet-name>
   <url-pattern>/session</url-pattern>
 </servlet-mapping>
</web-app>

3.JDBC

jdbc.html

<html>
<title>JDBC</title>
<head> FILL THE FOLLOWING BLANKS</head>
<form action="http://localhost:8081/jdbc1/jdbc" method="get">
<table>
<tr>
<td>Name: </td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Age: </td>
<td><input name="age" type="text"></td>
</tr>
<tr>
<td><input value="Ok" type="submit"></td>
<td><input value="Cancel" type="reset"></td>
</tr>
</table>
</form>
</html>
jdbc1.java

import javax.servlet.*;
import java.io.*;
import java.sql.*;


public class jdbc1 extends GenericServlet
{
  public void service(ServletRequest request,ServletResponse response) throws IOException,ServletException
  {
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
   
    String name=request.getParameter("name");
    int age=Integer.parseInt(request.getParameter("age"));
    out.println("<b>From Request Parameter: </b><br>Name: "+name+"<br>Age: "+age);
  
    out.println("<br><b>From DATABASE: </b><br>");
    String url = "jdbc:mysql://localhost:3306/jdbc?user=root&password=admin";
                                Connection conn;
                                ResultSet rs;      
                                try
                                {
                                                Class.forName("com.mysql.jdbc.Driver");
                                                conn = DriverManager.getConnection(url);
                                                Statement statement = conn.createStatement();
                        statement.executeUpdate("insert into jdbc values('"+name+"','"+age+"')");
                                                out.println("<table border=1><tr> <th>Name</th><th>Age</th></tr>");
                                                rs = statement.executeQuery("select * from jdbc");
                                                while(rs.next())
                                                {
                                                                out.println("<tr><td>" + rs.getString(1) + "</td><td> " + rs.getInt(2) + "</td></tr>");
                                                }
                                                out.println("</table>");
                                                rs.close();
                                                statement.close();
                                }
                                catch (Exception e)
                                {
                                                System.out.println(e);
                                }
  }

}

web.xml

<web-app>
  <servlet>
    <servlet-name>jdbc</servlet-name>
    <servlet-class>jdbc1</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>jdbc</servlet-name>
   <url-pattern>/jdbc</url-pattern>
 </servlet-mapping>
</web-app>



No comments:

Post a Comment