You are on page 1of 12

MCA-506 (based on MCA-501)

1312756

Program 10: Write a Java Servlet to display a welcome message as a response to the web
browser request for that servlet. Servlet should be stored at TOMCAT web server.
http://www.scribd.com/doc/61290146/Mobile-IP-goals-entities-packet-delivery-encapsulationstunnelling-and-DHCP-Unit-3
https://www.scribd.com/doc/94459343/Mobile-Computing-Unit-5
Coding:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class showMsg extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><title></title><body>");
out.println("Welcome ");
out.println("</body>");
out.println("</html>");
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)

1312756

Program 11: Write a Java Servlet to display the result of a student. The student information
containing Roll no., Name and marks of three subjects, is to be supplied through an HTML
FORM by the client.

showDataFromClient.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<form action="NewServlet" method="POST">
Name: <input type="text" name="name">
<br />
Roll: <input type="text" name="roll" />
<br/>
Marks:
<br/>
MCA501<input type="text" name="sub1">
MCA502<input type="text" name="sub2">
MCA503<input type="text" name="sub3">
<input type="submit" value="Submit" />
</form>
</body>
</html>
showDataFromClient.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
float x=(Integer.parseInt(request.getParameter("sub1"))+
Integer.parseInt(request.getParameter("sub2"))
+Integer.parseInt(request.getParameter("sub3")))/3;
String docType ="";
out.println(docType + "<html>\n" + "<head><title>Student Data</title></head>\n" +
"<h1>Student Information</h1>\n" + "<ul>\n" + "<li><b>Name</b>: " +
request.getParameter("name") + "\n" + " <li><b>Roll</b>: " + request.getParameter("roll")
+ "\n" +
" <li><b>Marks is (in MCA501,MCA502,MCA503) %</b>: " + x + "\n" +
"</ul>\n" +
"</body></html>");
M.M Institute of Computer Technology & Business Management,
Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)


}
}

OUTPUT :

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

1312756

MCA-506 (based on MCA-501)

Program 12: Write A Program using servlet by using doGet() method.


Newhtm.html
<html><head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<div>
<form action="doGetMethod" method="get">
Enter Name :&nbsp;&nbsp <input type="text" name="num" >
<input type="submit" value="submit">
</form>
</div></body></html>
doGetMethod.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class doGetMethod extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String num=request.getParameter("num");
PrintWriter out=response.getWriter();
out.println("Welcome "+num);
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

1312756

MCA-506 (based on MCA-501)

1312756

Program 13: Write A Program using servlet by using doPost() method.


Newhtm.html
<html> <head> <title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head> <body>
<div>
<form action="doPostMethod" method="post">
Enter Name :&nbsp;&nbsp <input type="text" name="num" >
<input type="submit" value="submit">
</form>
</div> </body></html>
doPostMethod.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class doPostMethod extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw=response.getWriter();
String val=request.getParameter("num");
pw.println("Welcome "+val+" Your ID is "+new Random().nextInt(100)) ;
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)

1312756

Program 14: Write a Java Servlet to Show the Student Record from the Database using JDBC
connection.

studDBShow.html
<!DOCTYPE html>
<html><head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<form action="studDBDataShow" method="GET">
<div>
<div>Enter UserID <input type="text" name="uid"><div>
<div>Enter Pass <input type="text" name="pass"><div>
<div><input type="submit" title="submit" value="submit"><div>
</div></form></body></html>
studDBShow.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.*;
import java.sql.*;
public class studDBDataShow extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection cn;
PrintWriter out = response.getWriter();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:database",request.getParameter("uid"),request.
getParameter("pass"));
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from student_Table");
String doc ="<html>\n" + "<head><title>Student Data</title></head>\n" +
"<h1>Student Information</h1>\n" + "<ul>\n" ;
while(rs.next()){
doc += " <li><b>Name</b>: " + rs.getString(2) + "\n" + "&nbsp&nbsp
<b>Roll</b>: "
+ rs.getInt("roll") + "\n" + &nbsp&nbsp&nbsp<b>Marks in %</b>: "+
rs.getInt("marks") + "\n";
}
doc=doc+ "</ul></body></html>";
out.println(doc);
cn.close();
}
catch(Exception e){
e.printStackTrace();
M.M Institute of Computer Technology & Business Management,
Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)


}
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

1312756

MCA-506 (based on MCA-501)

1312756

Program 15: Write a Java Servlet program to Update the Record from the Database using
JDBC.

Newhtm.html
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form action="updateStuDataDB" method="get">
<div style="width: 20">Enter Roll</div> <input type="text" name="roll"><br>
<div style="width: 20">Marks</div> <input type="text" name="att"><br>
<input type="submit" value="submit">
</form>
</div>
</body>
</html>
updateStuDataDB.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class updateStuDataDB extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection cn;
PrintWriter pw = response.getWriter();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:database","system","subhajit");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from student_Table where
roll="+Integer.parseInt(request.getParameter("roll")));
String doc ="<html>\n" + "<head><title>Student Data</title></head>\n" +
"<h2>Student Information with New Record</h2>\n" + "<ul>\n" ;
while(rs.next())
doc +=" <li><b>Name</b>: "+ rs.getString("name") + "\n"
+"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Roll</b>: "+ rs.getInt("roll") + "\n"
+"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Marks in %</b>: "+ rs.getInt("attendence") +
"\n";
st.executeUpdate("update student_Table set
Attendence="+Integer.parseInt(request.getParameter("att"))+"where
roll="+Integer.parseInt(request.getParameter("roll")));
doc+="<h2>New Updated Student Information</h2>\n\n";
rs=st.executeQuery("select * from student_Table where
roll="+Integer.parseInt(request.getParameter("roll")));
M.M Institute of Computer Technology & Business Management,
Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)

1312756

while(rs.next())
doc +=" <li><b>Name</b>: "+ rs.getString("name") + "\n"
+"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Roll</b>: "+ rs.getInt("roll") + "\n"
+"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Marks in %</b>: "+ rs.getInt("attendence") +
"\n";
doc=doc+ "</ul></body></html>";
pw.println(doc);
cn.close();
}
catch(Exception e){
pw.println("hello");
}
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)

1312756

Program 16: Write a Java Servlet program to insert the Record from the Database using
JDBC.

Newhtm.html
<html><head><title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<div>
<form action="insertStuDataDB" method="get">
<div style="width: 20">Enter Roll</div> <input type="text" name="roll"><br>
<div style="width: 20">Name</div> <input type="text" name="name"><br>
<div style="width: 20">Marks</div> <input type="text" name="att"><br>
<input type="submit" value="submit">
</form>
</div> </body></html>
insertStuDataDB.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class insertStuDataDB extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection cn;
PrintWriter pw = response.getWriter();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:database","system","subhajit");
Statement st=cn.createStatement();
PreparedStatement ps=cn.prepareStatement("insert into student_Table values(?,?,?)");
ps.setInt(1,Integer.parseInt(request.getParameter("roll")));
ps.setString(2, request.getParameter("name"));
ps.setInt(3, Integer.parseInt(request.getParameter("att")));
ps.addBatch();
ps.executeBatch();
ResultSet rs=st.executeQuery("select * from student_Table");
String doc ="<html>\n" + "<head><title>Student Data</title></head>\n" +
"<h1>Student Information with New Record</h1>\n" + "<ul>\n" ;
while(rs.next()){
doc +=
" <li><b>Name</b>: "
+ rs.getString(2) + "\n" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Roll</b>: "
+ rs.getInt("roll") + "\n" +
"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>Marks in %</b>: "+
rs.getInt("attendence") + "\n";
}
doc=doc+ "</ul></body></html>";
pw.println(doc);
M.M Institute of Computer Technology & Business Management,
Maharishi Markandeshwar University,Mullana(Ambala)

MCA-506 (based on MCA-501)


cn.close();
}
catch(Exception e){}
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

1312756

MCA-506 (based on MCA-501)


Program 17: Write a Java Swing program to Display the Record in a Table.
Newhtm.html
package swingtable;
import javax.swing.*;
public class SwingTable {
public static void main(String[] args) {
Object[][] cellData = { { "1312756", "subhajit","70" } };
String[] columnNames = { "Roll", "Name","Marks" };
JTable table = new JTable(cellData, columnNames);
JFrame f = new JFrame();
f.setSize(300,300);
f.add(new JScrollPane(table));
f.setVisible(true);
}
}
OUTPUT

M.M Institute of Computer Technology & Business Management,


Maharishi Markandeshwar University,Mullana(Ambala)

1312756

You might also like