Más contenido relacionado
La actualidad más candente (20)
Similar a My java file (20)
My java file
- 1. Experiment-1<br />Aim:- Program to access a table “table1” from MS-Access using java code.<br />Program:-<br />import java.sql.*;<br /> public class Main {<br />public static void main(String[] args)<br /> {<br /> try {<br /> Class.forName(quot;
sun.jdbc.odbc.JdbcOdbcDriverquot;
);<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br /> try<br /> {<br /> Connection conn;<br /> Statement stmt;<br /> ResultSet rs;<br /> conn=DriverManager.getConnection(quot;
Jdbc:Odbc:anamikaquot;
,quot;
anamikaquot;
,quot;
anamikaquot;
);<br /> stmt=conn.createStatement();<br /> rs=stmt.executeQuery(quot;
Select * from table1quot;
);<br /> while(rs.next())<br /> {<br /> System.out.println(rs.getString(quot;
namequot;
));<br /> System.out.println(rs.getString(quot;
rollnoquot;
));<br /> }<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br />}<br />}<br />Output<br />Experiment:-2<br />Aim:- Program to access a table “table1” from derby database using java code.<br />Program:-<br />import java.sql.*;<br /> public class Main {<br />public static void main(String[] args)<br /> {<br /> try {<br /> Class.forName(quot;
org.apache.derby.jdbc.ClientDriver quot;
);<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br /> try<br /> {<br /> Connection conn;<br /> Statement stmt;<br /> ResultSet rs;<br /> conn=DriverManager.getConnection(quot;
jdbc:derby://localhost:1527/anamikaquot;
,quot;
anamikaquot;
,quot;
anamikaquot;
);<br /> stmt=conn.createStatement();<br /> rs=stmt.executeQuery(quot;
Select * from table1quot;
);<br /> while(rs.next())<br /> {<br /> System.out.println(rs.getString(quot;
namequot;
));<br /> System.out.println(rs.getString(quot;
rollnoquot;
));<br /> }<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br />}<br />}<br />Output:-<br />Experiment-3<br />Aim:-Program for invoking a remote method using interface.<br />Program:-<br />Product.java<br />import java.rmi.*;<br />public interface product extends Remote<br />{<br />public String getname() throws RemoteException;<br />}<br />Product_impl.java<br />import java.rmi.*;<br />import java.rmi.server.*;<br />public class product_impl extends UnicastRemoteObject implements product<br />{<br />public String name;<br />public product_impl(String name) throws RemoteException<br />{this.name=name;<br />}<br />public String getname()<br />{<br />return name;<br />}<br /> }<br />Productserver.java<br />import java.rmi.*;<br />import java.rmi.registry.*;<br />public class productserver<br />{<br />public static void main(String s[])<br />{<br />try<br />{<br />LocateRegistry.getRegistry(1099);<br />product_impl p1=new product_impl(quot;
javaquot;
);<br />Naming.rebind(quot;
xyzquot;
,p1);<br />}<br />catch(Exception e)<br />{<br />System.out.println(e);<br />}<br />}<br />}<br />Productclient.java<br />import java.rmi.*;<br />import java.rmi.registry.*;<br /> class productclient<br />{<br />public static void main(String s[])<br />{<br />try<br />{<br />product p=(product)Naming.lookup(quot;
rmi://localhost:1099/xyzquot;
);<br />System.out.println(p.getname());<br />}<br />catch(Exception e)<br />{<br />System.out.println(e);<br />}<br />}<br />}<br />Generating stub and skeleton<br />Start server<br />Start client<br />Experiment:-4<br />Aim:- Program that implement a simple servlet program<br />Program:-<br />//Prog for simple servelet<br />import java.io.IOException;<br />import java.io.PrintWriter;<br />import javax.servlet.ServletException;<br />import javax.servlet.annotation.WebServlet;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />/**<br /> *<br /> * @author anamika<br /> */<br />@WebServlet(name=quot;
NewServletquot;
, urlPatterns={quot;
/NewServletquot;
})<br />public class NewServlet extends HttpServlet {<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType(quot;
text/html;charset=UTF-8quot;
);<br /> PrintWriter out = response.getWriter();<br /> try {<br /> // TODO output your page here<br /> out.println(quot;
<html>quot;
);<br /> out.println(quot;
<head>quot;
);<br /> out.println(quot;
<title>Servlet NewServlet</title>quot;
); <br /> out.println(quot;
</head>quot;
);<br /> out.println(quot;
<body>quot;
);<br /> out.println(quot;
<h1>Servlet NewServlet at quot;
+ request.getContextPath () + quot;
</h1>quot;
);<br /> out.println(quot;
</body>quot;
);<br /> out.println(quot;
This is my first servlet programquot;
);<br /> out.println(quot;
</html>quot;
);<br /> <br /> } finally { <br /> out.close();<br /> }<br /> } <br />Output<br /> <br />Experiment 5<br />Program:-servlet connectivity wid derby<br />import java.io.*;<br />import java.sql.*;<br />import javax.servlet.ServletConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.*;<br />/**<br /> *<br /> * @author student<br /> * @version<br /> */<br />public class NewServlet extends HttpServlet {<br /> Connection dbconnection;<br /> ResultSet resultset;<br /> /** Initializes the servlet.<br /> */<br /> public void init() throws ServletException {<br /> ServletConfig config = getServletConfig();<br /> String driverClassName = config.getInitParameter(quot;
driverclassnamequot;
);<br /> String dbURL = config.getInitParameter(quot;
dburlquot;
);<br /> String username = config.getInitParameter(quot;
usernamequot;
);<br /> String dbpassword = config.getInitParameter(quot;
dbpasswordquot;
);<br /> try<br /> {<br /> Class.forName(driverClassName);<br /> }<br /> catch(ClassNotFoundException cnfe)<br /> {<br /> System.err.println(quot;
Error loading driver: quot;
+ cnfe);<br /> }<br /> try<br /> {<br /> dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println(quot;
Connection error: quot;
+ sqle);<br /> }<br /> }<br /> /** Destroys the servlet.<br /> */<br /> public void destroy() {<br /> }<br /> /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType(quot;
text/htmlquot;
);<br /> PrintWriter out = response.getWriter();<br /> //TODO output your page here<br /> out.println(quot;
<html>quot;
);<br /> out.println(quot;
<head>quot;
);<br /> out.println(quot;
<title>Servlet</title>quot;
);<br /> out.println(quot;
</head>quot;
);<br /> out.println(quot;
<body>quot;
);<br /> try<br /> {<br /> Statement statement = dbconnection.createStatement();<br /> String sqlString = quot;
SELECT * FROM app.untitledquot;
;<br /> resultset=statement.executeQuery(sqlString);<br /> while(resultset.next())<br /> {<br /> out.println(resultset.getString(quot;
namequot;
));<br /> out.println(resultset.getString(quot;
rollnoquot;
));<br /> // password = resultset.getString(quot;
UserPasswordquot;
);<br /> }<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println(quot;
Connection error: quot;
+ sqle);<br /> }<br /> // out.println(quot;
subahshquot;
);<br /> out.println(quot;
</body>quot;
);<br /> out.println(quot;
</html>quot;
);<br /> out.close();<br /> }<br /> /** Handles the HTTP <code>GET</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doGet(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Handles the HTTP <code>POST</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doPost(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Returns a short description of the servlet.<br /> */<br /> public String getServletInfo() {<br /> return quot;
Short descriptionquot;
;<br /> }<br />}<br />Web.xml<br /><?xml version=quot;
1.0quot;
encoding=quot;
UTF-8quot;
?><br /><web-app version=quot;
3.0quot;
xmlns=quot;
http://java.sun.com/xml/ns/javaeequot;
xmlns:xsi=quot;
http://www.w3.org/2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdquot;
><br /> <servlet><br /> <servlet-name>NewServlet</servlet-name><br /> <servlet-class>NewServlet</servlet-class><br /> <init-param><br /> <param-name>driverclassname</param-name><br /> <param-value>org.apache.derby.jdbc.ClientDriver</param-value><br /> </init-param><br /> <init-param><br /> <param-name>dburl</param-name><br /> <param-value>jdbc:derby://localhost:1527/anamika</param-value><br /> </init-param><br /> <init-param><br /> <param-name>username</param-name><br /> <param-value>anamika</param-value><br /> </init-param><br /> <init-param><br /> <param-name>dbpassword</param-name><br /> <param-value>anamika</param-value><br /> </init-param><br /> </servlet><br /> <servlet-mapping><br /> <servlet-name>NewServlet</servlet-name><br /> <url-pattern>/NewServlet</url-pattern><br /> </servlet-mapping><br /> <session-config><br /> <session-timeout><br /> 30<br /> </session-timeout><br /> </session-config><br /> <welcome-file-list><br /> <welcome-file>index.jsp</welcome-file><br /> </welcome-file-list><br /> </web-app><br />Output<br />Experiment:-login using servelts<br />import java.io.*;<br />import java.sql.*;<br />import javax.servlet.ServletConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.*;<br />/**<br /> *<br /> * @author student<br /> * @version<br /> */<br />public class NewServlet extends HttpServlet {<br /> Connection dbconnection;<br /> ResultSet resultset;<br /> /** Initializes the servlet.<br /> */<br /> String userid;<br /> String password;<br /> public void init() throws ServletException {<br /> ServletConfig config = getServletConfig();<br /> String driverClassName = config.getInitParameter(quot;
driverclassnamequot;
);<br /> String dbURL = config.getInitParameter(quot;
dburlquot;
);<br /> String username = config.getInitParameter(quot;
usernamequot;
);<br /> String dbpassword = config.getInitParameter(quot;
dbpasswordquot;
);<br /> try<br /> {<br /> Class.forName(driverClassName);<br /> }<br /> catch(ClassNotFoundException cnfe)<br /> {<br /> System.err.println(quot;
Error loading driver: quot;
+ cnfe);<br /> }<br /> try<br /> {<br /> dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println(quot;
Connection error: quot;
+ sqle);<br /> }<br /> }<br /> /** Destroys the servlet.<br /> */<br /> public void destroy() {<br /> }<br /> /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType(quot;
text/htmlquot;
);<br /> PrintWriter out = response.getWriter();<br /> // String s1=request.getParameter(quot;
t1quot;
);<br /> // String ps=request.getParameter(quot;
p1quot;
);<br /> try<br /> {<br /> Statement statement = dbconnection.createStatement();<br /> String sqlString = quot;
SELECT * FROM app.student1 WHERE username='quot;
+request.getParameter(quot;
useridquot;
)+quot;
'AND password='quot;
+request.getParameter(quot;
passquot;
)+quot;
'quot;
;<br /> resultset=statement.executeQuery(sqlString);<br /> while(resultset.next())<br /> {<br /> userid = resultset.getString(quot;
usernamequot;
);<br /> password = resultset.getString(quot;
passwordquot;
);<br /> }<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println(quot;
Connection error: quot;
+ sqle);<br /> }<br /> if(password.equals(request.getParameter(quot;
passquot;
)) && userid.equals(request.getParameter(quot;
useridquot;
)))<br /> {<br /> response.sendRedirect(quot;
/WebApplication2/correct.htmlquot;
);<br /> }<br /> else<br /> {<br /> response.sendRedirect(quot;
/WebApplication2/incorrect.htmlquot;
);<br /> }<br /> //TODO output your page here<br /> out.println(quot;
<html>quot;
);<br /> out.println(quot;
<head>quot;
);<br /> out.println(quot;
<title>Servlet</title>quot;
);<br /> out.println(quot;
</head>quot;
);<br /> out.println(quot;
<body>quot;
);<br /> // out.println(s1);<br /> // out.println(ps);<br /> out.println(quot;
</body>quot;
);<br /> out.println(quot;
</html>quot;
);<br /> out.close();<br /> }<br /> /** Handles the HTTP <code>GET</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doGet(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Handles the HTTP <code>POST</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doPost(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Returns a short description of the servlet.<br /> */<br /> public String getServletInfo() {<br /> return quot;
Short descriptionquot;
;<br /> }<br />}<br />Mytable is student1 wid username,password<br />loginframe<br />Top of Form<br />Top of Form<br /><form action=quot;
/Program6/NewServletquot;
method=quot;
POSTquot;
><br /> <h1>UserId<input type=quot;
textquot;
name=quot;
useridquot;
value=quot;
quot;
size=quot;
20quot;
/> <br /> Password<input type=quot;
passwordquot;
name=quot;
passquot;
value=quot;
quot;
size=quot;
20quot;
/><br /> <input type=quot;
submitquot;
value=quot;
loginquot;
/></h1><br />Correct.html<br />Incorrect.html<br />Web.xml:-same as above<br />Experiment:-login using jsp<br />create jsp and insert code of loginframe into its body.rest all is same as above and delete correct.html<br />Experiment:-session management<br />Changes are only underlined <br />/*<br /> * To change this template, choose Tools | Templates<br /> * and open the template in the editor.<br /> */<br />import java.io.*;<br />import java.sql.*;<br />import javax.servlet.ServletConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.*;<br />/**<br /> *<br /> * @author student<br /> * @version<br /> */<br />public class NewServlet extends HttpServlet {<br /> Connection dbconnection;<br /> ResultSet resultset;<br /> /** Initializes the servlet.<br /> */<br /> String userid;<br /> String password;<br /> public void init() throws ServletException {<br /> ServletConfig config = getServletConfig();<br /> String driverClassName = config.getInitParameter(quot;
driverclassnamequot;
);<br /> String dbURL = config.getInitParameter(quot;
dburlquot;
);<br /> String username = config.getInitParameter(quot;
usernamequot;
);<br /> String dbpassword = config.getInitParameter(quot;
dbpasswordquot;
);<br /> try<br /> {<br /> Class.forName(driverClassName);<br /> }<br /> catch(ClassNotFoundException cnfe)<br /> {<br /> System.err.println(quot;
Error loading driver: quot;
+ cnfe);<br /> }<br /> try<br /> {<br /> dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println(quot;
Connection error: quot;
+ sqle);<br /> }<br /> }<br /> /** Destroys the servlet.<br /> */<br /> public void destroy() {<br /> }<br /> /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType(quot;
text/htmlquot;
);<br /> PrintWriter out = response.getWriter();<br /> // String s1=request.getParameter(quot;
t1quot;
);<br /> // String ps=request.getParameter(quot;
p1quot;
);<br /> HttpSession s=request.getSession();<br /> try<br /> {<br /> Statement statement = dbconnection.createStatement();<br /> String sqlString = quot;
SELECT * FROM app.STUDENT WHERE userid='quot;
+request.getParameter(quot;
useridquot;
)+quot;
'AND pass='quot;
+request.getParameter(quot;
passquot;
)+quot;
'quot;
;<br /> resultset=statement.executeQuery(sqlString);<br /> while(resultset.next())<br /> {<br /> userid = resultset.getString(quot;
useridquot;
);<br /> password = resultset.getString(quot;
passquot;
);<br /> }<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println(quot;
Connection error: quot;
+ sqle);<br /> }<br /> if(password.equals(request.getParameter(quot;
passquot;
)) && userid.equals(request.getParameter(quot;
useridquot;
)))<br /> {<br /> response.sendRedirect(quot;
/WebApplication3/correct.jsp?ssid=quot;
+s.getId()+quot;
quot;
);<br /> }<br /> else<br /> {<br /> response.sendRedirect(quot;
/WebApplication3/incorrect.jsp?ssid=quot;
+s.getId()+quot;
quot;
);<br /> }<br /> //TODO output your page here<br /> out.println(quot;
<html>quot;
);<br /> out.println(quot;
<head>quot;
);<br /> out.println(quot;
<title>Servlet</title>quot;
);<br /> out.println(quot;
</head>quot;
);<br /> out.println(quot;
<body>quot;
);<br /> // out.println(s1);<br /> // out.println(ps);<br /> out.println(quot;
</body>quot;
);<br /> out.println(quot;
</html>quot;
);<br /> out.close();<br /> }<br /> /** Handles the HTTP <code>GET</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doGet(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Handles the HTTP <code>POST</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doPost(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Returns a short description of the servlet.<br /> */<br /> public String getServletInfo() {<br /> return quot;
Short descriptionquot;
;<br /> }<br />}<br /><%-- <br /> Document : correct<br /> Created on : Apr 10, 2011, 3:18:30 AM<br /> Author : babu<br />--%><br /><%@page contentType=quot;
text/htmlquot;
pageEncoding=quot;
UTF-8quot;
%><br /><!DOCTYPE HTML PUBLIC quot;
-//W3C//DTD HTML 4.01 Transitional//ENquot;
<br /> quot;
http://www.w3.org/TR/html4/loose.dtdquot;
><br /><html><br /> <head><br /> <meta http-equiv=quot;
Content-Typequot;
content=quot;
text/html; charset=UTF-8quot;
><br /> <title>JSP Page</title><br /> </head><br /> <body><br /> <%!<br />String userid;<br />String in_session;<br />%><br /><%<br />userid=request.getParameter(quot;
ssidquot;
);<br />String Id=session.getId();<br />//out.println(userid);<br />//out.println(Id);<br />//if(Id.equals(userid))<br /> if(request.getParameter(quot;
ssidquot;
).equals(session.getId()))<br />{%><br /> correct yipee<br /> <% }<br />else{<br /> %><br /> // response.sendRedirect(quot;
abc.htmlquot;
);<br /> <jsp:forward page=quot;
abc.htmlquot;
/><br /> <%<br />}<br />session.invalidate();<br />%><br /> </body><br /></html><br />Same of incorrect.jsp only make correct inplace of correct<br />Web.xml same as previous<br />Output<br />This above url pasted on another tab and we get d output<br />Experiment:-jsp connectivity<br /><%-- <br /> Document : jspconnectivity<br /> Created on : Apr 10, 2011, 3:38:37 AM<br /> Author : babu<br />--%><br /><%@page language=quot;
javaquot;
import=quot;
java.sql.*quot;
%><br /><!DOCTYPE HTML PUBLIC quot;
-//W3C//DTD HTML 4.01 Transitional//ENquot;
<br /> quot;
http://www.w3.org/TR/html4/loose.dtdquot;
><br /><html><br /> <head><br /> <meta http-equiv=quot;
Content-Typequot;
content=quot;
text/html; charset=UTF-8quot;
><br /> <title>JSP Page</title><br /> </head><br /> <body><br /> <%<br /> try {<br /> Class.forName(quot;
org.apache.derby.jdbc.ClientDriverquot;
);<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br /> try<br /> {<br /> Connection conn;<br /> Statement stmt;<br /> ResultSet rs;<br /> conn=DriverManager.getConnection(quot;
jdbc:derby://localhost:1527/anamikaquot;
,quot;
anamikaquot;
,quot;
anamikaquot;
);<br /> stmt=conn.createStatement();<br /> rs=stmt.executeQuery(quot;
Select * from app.studentquot;
);<br /> // PrintStream out= new PrintStream(response.getOutputStream());<br /> //PrintStream out= new PrintStream(response.getOutputStream());<br /> //response.setContentType(quot;
text/htmlquot;
);<br /> //response.setContentType(quot;
text/htmlquot;
);<br /> out.println(quot;
<html>quot;
);<br /> out.println(quot;
<head>quot;
);<br /> out.println(quot;
<title>My table</title>quot;
);<br /> out.println(quot;
<title>my personal details</title>quot;
);<br /> out.println(quot;
</head>quot;
);<br /> out.println(quot;
<body>quot;
);<br /> out.println(quot;
<center>quot;
);<br />//out.println(“<center>”);<br /> out.print(quot;
<table border=1>quot;
);<br /> out.print(quot;
<td>userid</td>quot;
);<br /> out.print(quot;
<td>password</td>quot;
);<br /> // System.out.println(quot;
Rollno Namequot;
);<br /> while(rs.next())<br /> {<br /> //out.println(rs.getString(quot;
useridquot;
));<br /> // out.println(rs.getString(quot;
passquot;
));<br /> out.print(quot;
<tr>quot;
);<br /> out.print(quot;
<td>quot;
+rs.getString(1));<br /> String s=rs.getString(2);<br /> out.print(quot;
<td>quot;
+s);<br /> }<br /> //out.println(“</center>”);<br /> out.println(quot;
</center>quot;
);<br /> out.println(quot;
</table>quot;
);<br /> out.println(quot;
</body>quot;
);<br /> out.println(quot;
</html>quot;
);<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br />%><br /> </body><br /></html><br />Output<br />