Advanced java practical semester 6_computer science

Post on 27-Jan-2017

195 views 4 download

Transcript of Advanced java practical semester 6_computer science

T.Y.B.Sc. Computer Science Roll No.

INDEX

Practical No. Practical Name Page No.

Date Remark Signature

1. Simple Server-Side Programming using Servlets . Where user can get the information about server.

01 to 03 05/12/2015

2. Advance Server-Side Programming using Servlets.

Where user can get details of his created table in SQL.

04 to 08 12/12/2015

3. Simple Server-side programming using JSP for Login page where after login the page forwarded to new welcome page and if login

details are wrong then page transferred to index page.

09 to 12 19/12/2015

4. Advance Server-side programming using JSP for login page where login details for user taken from database created in SQL. If user_id and password entered by user is wrong then

page redirected to invalid page or else redirected to Home page.

13 to 20 02/01/2016

5. Developing Simple Enterprise Java Beans for addition of two numbers by taking inputs from

user.

21 to 25 09/01/2016

6. Developing Advance Enterprise Java Beans. Where user is

entering details of registration form in Registration.jsp file and

the data entered by user should be stored in database.

26 to 29 23/01/2016

7. Developing Simple Web services in Java Demonstrate a simple web service that generates a response based on information received

from the client.

30 to 34 06/02/2016

8. Developing Advance Web services in Java. Develop web

service for calculator applications, to implement simple arithmetic

Operations. Use WSDL for these web services to show output.

35 to 39 13/02/2016

T.Y.B.Sc. Computer Science Roll No.

Practical No: 1

Practical Name :–

Simple Server-Side Programming using Servlets. Where user can get the information about server.

Index.jsp :-

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<form action="serverinfo" method="post"><input type="submit" value="submit">

</form>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Serverinfo.java :-

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

public class extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try {

out.println("<html>");out.println("<head>");

out.println("<h3>Server Info:<h3>");out.println("Server Name:"+request.getServerName()+"<br><br>");out.println("Server Port:"+request.getServerPort()+"<br><br>");

out.println("Client Browser:"+request.getHeader("User Agent")+"<br><br>");out.println("Server Referer:"+request.getHeader("Referer")+"<br><br>");out.println("Client IP:"+request.getRemoteAddr()+"<br><br>");out.println("Server OS Version:"+System.getProperty("os.version")+"<br><br>");out.println("</head>");out.println("<body>");

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

} finally { out.close();

}}

}

T.Y.B.Sc. Computer Science Roll No.

Output :-

T.Y.B.Sc. Computer Science Roll No.

Practical No: 2

Practical Name :–

Advance Server-Side Programming using Servlets. Where user can get details of his created table in SQL.

Index.jsp :-

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<form action="dataconnection1" method="POST"><input type="submit" value="submit">

</form>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Dataconnection1.java :-

import java.io.IOException;import java.io.PrintWriter;import java.sql.*;import javax.sql.*;import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "dataconnection1", urlPatterns = {"/dataconnection1"})public class dataconnection1 extends HttpServlet implements Servlet{

protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();

try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");out.println("connectivity begins");

Connection cn;

Statement st;ResultSet rs;

cn=DriverManager.getConnection("jdbc:odbc:ServletDSN","system","dbms");out.println("cooncted successfully");st=cn.createStatement();st.executeUpdate("create table Aut1(AuId Number,AuName Char(15),AuAddr

Char(30))");out.println("Table Created");st.executeUpdate("insert into Aut1 values(1,'ABC','Panvel')");st.executeUpdate("insert into Aut1 values(2,'XYZ','Pune')");out.println("Records inserted into table");rs=st.executeQuery("select * from Aut1");out.println("AuId &nbsp &nbsp AuName &nbsp &nbsp AuAddr <br>");

T.Y.B.Sc. Computer Science Roll No.

while(rs.next()){out.println(rs.getInt(1)+"&nbsp &nbsp &nbsp");out.println(rs.getString(2)+"&nbsp &nbsp &nbsp");out.println(rs.getString(3)+"&nbsp &nbsp &nbsp");

st.close(); }rs.close();cn.close();

out.println("<html>");out.println("<head>");out.println("<title>Servlet dataconnection1</title>"); out.println("</head>");out.println("<body>");out.println("<h1>Servlet dataconnection1 at " + request.getContextPath() + "</h1>");out.println("</body>");out.println("</html>");

}catch(Exception e1){

System.out.println("Failed");}finally {

out.close();}

}}

T.Y.B.Sc. Computer Science Roll No.

Output:-

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Practical No: 3

Practical Name:–

Simple Server-side programming using JSP for Login page where after login the page forwarded to new welcome page and if login details are wrong then page transferred to index page.

Index.jsp :-

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<form method="post" action="login1.jsp">username<input type="text" name="txtname" size="20"><br>password<input type="password" name="pwd" size="20"><br><input type="submit" value="Login">

</form></body>

</html>

T.Y.B.Sc. Computer Science Roll No.

Login1.jsp :-

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<%if((request.getParameter("txtname").equals("yogita"))&&(request.getParameter("pwd").equals("vyom"))){%><jsp:forward page="welcome.jsp"></jsp:forward>

<%} else{ out.println("Enter correct details");%><jsp:include page="index.jsp"></jsp:include><%}%>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Login1.jsp :-

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<h1>Hello World!</h1></body>

</html>

T.Y.B.Sc. Computer Science Roll No.

Output:-

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Practical No: 4

Practical Name:–

Advance Server-side programming using JSP for login page where login details for user taken from database created in SQL. If user_id and password entered by user is wrong then page redirected to invalid page or else redirected to Home page.

Index.jsp :-

<%--Document : indexCreated on : Mar 8, 2016, 10:54:04 AMAuthor : computerlab

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<form action="TLogin.jsp" method="post"><table bgcolor="yellow"><tr><td>User Name:<input type="text" name="userid" size="15"></td></tr><tr><td>Password:<input type="password" name="pwd" size="15"></td></tr><tr><td colspan="2" align="center"><input type="submit" name="submit"

size="10"></td></tr></table>

</form></body>

</html>

T.Y.B.Sc. Computer Science Roll No.

TLogin.jsp :-

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@page import="java.sql.*"%><%@page import="javax.sql.*"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body><%Connection con;String userid=request.getParameter("userid");out.println("hellow"+userid);session.setAttribute("userid",userid);String pwd=request.getParameter("pwd");Class.forName("com.mysql.jdbc.Driver");con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");out.println("connected successfully");

Statement st=con.createStatement();ResultSet rs=st.executeQuery("select * from student where userid='"+userid+"'");if(rs.next()){if(rs.getString(2).equals(pwd)){

//System.out.println("Login sucessfully");response.sendRedirect("Home.jsp");

}else{response.sendRedirect("Invalid.jsp");} }

%>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Invalid.jsp :-

<%--Document : InvalidCreated on : Mar 8, 2016, 10:56:04 AMAuthor : computerlab

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<body bgcolor="pink"><h1>Invalid User ID or Password<%=session.getAttribute("userid")%>!!!</h1>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Home.jsp :-

<%--Document : HomeCreated on : Feb 23, 2016, 9:06:59 AMAuthor : computerlab

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<body bgcolor="pink"><h1>Welcome<%=session.getAttribute("userid")%>!!!</h1>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Output:-

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Practical No: 5

Practical Name:–

Developing Simple Enterprise Java Beans for addition of two numbers by taking inputs from user.

Index.jsp :-

<%--Document : indexCreated on : Feb 12, 2016, 9:08:58 AMAuthor : computelab

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<form method="post" action="calcservlet"><input type="text" name="t1"><input type="text" name="t2"><input type="submit" value="OK">

</form></body>

</html>

T.Y.B.Sc. Computer Science Roll No.

calcservlet.java :-

import ejb.calbeanLocal;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

public class calcservlet extends HttpServlet {

calbeanLocal calbean=new calbeanLocal();

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try {

out.println("<html>");out.println("<head>");out.println("<title>Servlet calcservlet</title>"); out.println("</head>");out.println("<body>");int a=Integer.parseInt(request.getParameter("t1"));int b=Integer.parseInt(request.getParameter("t2"));out.println("<h1>Sum ="+calbean.addition(a,b)+" </h1>");out.println("</body>");out.println("</html>");

} finally { out.close();

}}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

/*** Handles the HTTP* <code>GET</code> method.** @param request servlet request* @param response servlet response* @throws ServletException if a servlet-specific error occurs

T.Y.B.Sc. Computer Science Roll No.

* @throws IOException if an I/O error occurs*/@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {processRequest(request, response);

}

/*** Handles the HTTP* <code>POST</code> method.** @param request servlet request* @param response servlet response* @throws ServletException if a servlet-specific error occurs* @throws IOException if an I/O error occurs*/@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {processRequest(request, response);

}

/*** Returns a short description of the servlet.** @return a String containing servlet description*/@Overridepublic String getServletInfo() {

return "Short description";}// </editor-fold>

}

T.Y.B.Sc. Computer Science Roll No.

calbeanLocal.java :-

package ejb;

import javax.ejb.LocalBean;import javax.ejb.Stateless;

@Stateless@LocalBeanpublic class calbeanLocal {

public Integer addition(int a, int b) {return (a+b);

}

}

T.Y.B.Sc. Computer Science Roll No.

Output:-

T.Y.B.Sc. Computer Science Roll No.

Practical No: 6

Practical Name:–

Developing Advance Enterprise Java Beans. Where user is entering details of registration form in Registration.jsp file and the data entered by user should be stored in database.

Index.jsp:

<%--Document : indexCreated on : Mar 2, 2016, 12:10:46 PMAuthor : computerlab

--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body>

<form action="Registeration1.jsp" method="post">User Name: <input type="text" name="un"><br/>Password: <input type="text" name="tp"><br/>First Name: <input type="text" name="fn"><br/>Last Name: <input type="text" name="ln"><br/>Email: <input type="text" name="e"><br/><input type="submit" value="Register"><br/></form>

</body></html><%--

T.Y.B.Sc. Computer Science Roll No.

Registeration1.jsp:-

Created on : Aug 31, 2015, 9:41:20 AMAuthor : computerlab

--%>

<%@page import="java.sql.DriverManager"%><%@page import="java.sql.Connection"%><%@page import="java.sql.PreparedStatement"%><%@page contentType="text/html" pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html><html>

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title>

</head><body><%

String user_name=request.getParameter("un");String pswd=request.getParameter("tp");String first_name=request.getParameter("fn");String last_name=request.getParameter("ln");String email=request.getParameter("e");try{Class.forName("com.mysql.jdbc.Driver");Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/reg1","root","root");PreparedStatement ps = con.prepareStatement("insert into new_table values(?,?,?,?,?)");ps.setString(1, user_name);ps.setString(2, pswd);ps.setString(3, first_name);ps.setString(4, last_name);ps.setString(5, email);ps.executeUpdate();out.println("Successful Registration");}catch(Exception e){out.println("Exception caught"+e);}%>

</body></html>

T.Y.B.Sc. Computer Science Roll No.

Output:-

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Practical No: 7Practical Name:–

Developing Simple Web services in Java

Demonstrate a simple web service that generates a response based on information received from the client.

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Program :-

package server;

import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;

@WebService()public class HelloWebService{

@WebMethod(operationName = "sayHello")public String sayHello(@WebParam(name = "name")String name){

return "Hello -" + name;}

}

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Practical No: 8Practical Name:–

Developing Advance Web services in Java.

Develop web service for calculator applications, to implement simple arithmeticOperations. Use WSDL for these web services to show output.

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.

Program :-

package server;

import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;

@WebService()public class CalcWebService{

@WebMethod(operationName = "add")public int add(

@WebParam(name = "i")int i, @WebParam(name = "j")int j) {

return i+j;}}

T.Y.B.Sc. Computer Science Roll No.

T.Y.B.Sc. Computer Science Roll No.