Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Practical (Traffic Website)
- Index.html
- <html>
- <head>
- <title>TODO supply a title</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <form action="insert">
- Vehicle Number : <input type="text" name="vname">
- Complaint : <input type ="text" name="vcom">
- Name : <input type="text" name="naam">
- <input type="submit">
- </form>
- </body>
- </html>
- insert.java (Servlet):
- import java.sql.*;
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- //Step 1
- String vehicle = request.getParameter("vname");
- String com = request.getParameter("vcom");
- String naam = request.getParameter("naam");
- //Step 2
- String dsn = "jdbc:derby://localhost:1527/Traffic";
- String user = "root";
- String pass = "root";
- try{
- Connection c = DriverManager.getConnection(dsn,user,pass);
- //Insert Into Vehicle Values('vid','com','naam')
- String sql = "Insert into Vehicle Values('" +vehicle+ "','" +com+ "','" +naam+ "')";
- Statement s = c.createStatement();
- s.executeUpdate(sql);
- }catch(Exception e){
- System.out.println(e);
- }
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet insert</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet insert at Data INserted</h1>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- Check Data Inserted or not in Service dsn Tables Table_NameView Data
- View.html :
- <html>
- <head>
- <title>TODO supply a title</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <form action="viewData.jsp">
- ENter Number : <input type="text" name="vnum">
- <input type="submit">
- </form>
- </body>
- </html>
- viewData.java (Servlet):
- import java.sql.*;
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- //Step 1
- String vid = request.getParameter("vnum");
- //Step 2
- String dsn = "jdbc:derby://localhost:1527/Traffic";
- String user = "root";
- String pass = "root";
- try{
- Connection c = DriverManager.getConnection(dsn,user,pass);
- //Select Vid,Complaint,Name From Vehicle where vid = 'vid'
- String sql = "Select Vid,Complaint,Name From Vehicle where Vid = '" +vid+ "'";
- Statement s = c.createStatement();
- ResultSet rs = s.executeQuery(sql);
- rs.next();
- String vnum = rs.getString("VID");
- String comp = rs.getString("Complaint");
- String name= rs.getString("Name");
- //Response
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet viewData</title>");
- out.println("</head>");
- out.println("<body>");
- out.println(vnum+ " " +comp+ " " +name);
- out.println("</body>");
- out.println("</html>");
- }catch(Exception e){
- System.out.println(e);
- }}}
- //Practical (Table using jsp)
- Index.html :
- <html>
- <head>
- <title>TODO supply a title</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <form action="newjsp.jsp">
- ENter Number : <input type="text" name="vnum">
- <input type="submit">
- </form>
- </body>
- </html>
- newjsp.jsp (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>
- <table>
- <%
- String n = request.getParameter("vnum");
- int num = Integer.parseInt(n);
- //Niche Jaane ki liye
- for(int i=1;i<=10;i++){
- //Agge Baadhne ke liye
- out.println("<tr>");
- for(int j=1;j<=num;j++){
- out.println("<td>" +(i*j)+ "</td>");
- }
- out.println("</tr>");
- }
- %>
- </table>
- </body>
- </html>
- //Practical (Chat )
- Client jFrame banake..
- Client.java
- import java.net.*;
- import java.io.*;
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- Socket s = new Socket("localhost",2000);
- DataOutputStream os = new DataOutputStream(s.getOutputStream());
- String data = jTextField1.getText();
- os.writeUTF(data);
- }catch(Exception e){
- System.out.println(e);
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- Socket s = new Socket("localhost",2000);
- DataInputStream is = new DataInputStream(s.getInputStream());
- String msg = is.readUTF();
- System.out.println(msg);
- }catch(Exception e){
- System.out.println(e);
- }
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- Create a new Project for Server…..
- Server > jFrame banake
- Server.java
- import java.net.*;
- import java.io.*;
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- ServerSocket ss = new ServerSocket(2000);
- Socket s = ss.accept();
- DataOutputStream os = new DataOutputStream(s.getOutputStream());
- String data = jTextField1.getText();
- os.writeUTF(data);
- }catch(Exception e){
- System.out.println(e);
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- ServerSocket ss = new ServerSocket(2000);
- Socket s = ss.accept();
- DataInputStream is = new DataInputStream(s.getInputStream());
- String msg = is.readUTF();
- System.out.println(msg);
- }catch(Exception e){
- System.out.println(e);
- }
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement