Q1: Write a program using Servlet and JDBC for developing an online submission of an examination form. You are required to create a database comprising of the following fields:I. Student Name II. Enrollment No. III. Course Code (s) IV. Regional Center Code V. E-mail Id
ANS:-
Student Name Enrollment No. Course Code (s) Regional Code
Center
E-mail Id
Registrationform.html
Student Registration Form
import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import java.sql.DriverManager; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class Student extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } Connection con; @Override public void init(ServletConfig sc) { try { DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); con=DriverManager.getConnection("jdbc:oracle:thin:@oraserv:1521:aptech"," scott","tiger"); } catch(Exception e) { e.printStackTrace(); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { PrintWriter out=response.getWriter(); String name=request.getParameter("sname"); String enrl=request.getParameter("enrol"); String ccode=request.getParameter("cocode"); String rccode=request.getParameter("eccode"); String email=request.getParameter("email"); if(name!=null && enrl!=null && ccode!=null && rccode!=null && email!=null) { Statement st=con.createStatement(); st.execute("insert into studreg values ('"+name+"','"+enrl+"','"+ccode+"','"+rccode+"','"+email+"')"); out.println("
Submited Success Fully
"); } else {
out.println("
Please Fill The All Field
"); } } catch(Exception e) { e.printStackTrace(); } } @Override public void destroy() { try { con.close(); } catch (SQLException ex) { Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex); } super.destroy(); }
@Override public String getServletInfo() { return "Short description"; } } Q4: Write a JSP program which displays a webpage containing arrival of new items within a particular month in the different branches of a retail company.
ANS:; <%@ page import="java.sql.*,java.text.*,java.util.*" %> <%@ page import="java.io.*" %>
Connection with mysql database
Connection status
Item Name | Company | Address |
<% try { Date date = new Date(); // Some examples Format formatter = new SimpleDateFormat("yyyy-MM-dd"); String currentdate = formatter.format(date); /* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is usermaster. */ String connectionURL = "jdbc:mysql://localhost:3306/usermaster";; // declare a connection by using Connection interface Connection connection = null; // Load JBBC driver "com.mysql.jdbc.Driver" Class.forName("com.mysql.jdbc.Driver").newInstance(); /* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */ connection = DriverManager.getConnection(connectionURL, "root", "root"); Statement stmt = connection.createStatement(); String query = "select * from table where itemdate='"+currentdate+"'"; ResultSet rs = stmt.executeQuery(query); while(rs.next()) { %> <%=rs.getString("item_name")%> | <%=rs.getString("company")%> | <%=rs.getString("address")%> |
<% } } catch(Exception ex){ %> <%
out.println("Unable to connect to database."); } %>
Q3: Using Servlet, JSP, JDBC and XML create a web application for a courrier company to provide online help in tracking the delivery status of items.
Ans:This ans is attached with servlet.zip.
Q7: How do you write SSL Client? Take example of SSL Socket Client.
Ans:import java.io.*; import java.net.*; import javax.net.ssl.*; public class SslSocketClient { public static void main(String[] args) { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); PrintStream out = System.out; SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault(); try { SSLSocket c = (SSLSocket) f.createSocket("localhost", 8888); printSocketInfo(c); c.startHandshake(); BufferedWriter w = new BufferedWriter( new OutputStreamWriter(c.getOutputStream())); BufferedReader r = new BufferedReader( new InputStreamReader(c.getInputStream())); String m = null; while ((m=r.readLine())!= null) { out.println(m); m = in.readLine(); w.write(m,0,m.length()); w.newLine();
w.flush(); } w.close(); r.close(); c.close(); } catch (IOException e) { System.err.println(e.toString()); } } private static void printSocketInfo(SSLSocket s) { System.out.println("Socket class: "+s.getClass()); System.out.println(" Remote address = " +s.getInetAddress().toString()); System.out.println(" Remote port = "+s.getPort()); System.out.println(" Local socket address = " +s.getLocalSocketAddress().toString()); System.out.println(" Local address = " +s.getLocalAddress().toString()); System.out.println(" Local port = "+s.getLocalPort()); System.out.println(" Need client authentication = " +s.getNeedClientAuth()); SSLSession ss = s.getSession(); System.out.println(" Cipher suite = "+ss.getCipherSuite()); System.out.println(" Protocol = "+ss.getProtocol()); } }