Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Practical (Traffic Complaint Website)
- index.html page
- <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>
- view.html page
- <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="view">
- Enter Number : <input type="text" name="vnum">
- <input type="submit">
- </form>
- </body>
- </html>
- insert.java file
- public class insert extends HttpServlet {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- //Initializing Variable for Storing Data
- String vehicle = request.getParameter("vname");
- String com = request.getParameter("vcom");
- String naam = request.getParameter("naam");
- //Connection of Database
- String dsn = "jdbc:derby://localhost:1527/Traffic";
- String user = "root";
- String pass = "root";
- try{
- Connection c = DriverManager.getConnection(dsn, user, pass);
- //Query
- 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>Data Inserted</h1>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- view.java File
- public class view extends HttpServlet {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- //Initialize Variable to store data
- String vehiclenum = request.getParameter("vnum");
- //Conntecting to Database
- String dsn = "jdbc:derby://localhost:1527/Traffic";
- String user = "root";
- String pass = "root";
- try{
- Connection c = DriverManager.getConnection(dsn,user,pass);
- //Select Vehicle,Complaint,Name from Vehicle where Vehicle="vehiclenum"
- String sql = "Select Vehicle,Complaint,Name from Vehicle where Vehicle='"+vehiclenum+"'";
- Statement s = c.createStatement();
- ResultSet rs = s.executeQuery(sql);
- rs.next();
- String vnum = rs.getString("Vehicle");
- 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 view</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h2>"+vnum+ " "+comp+ " "+name+"</h2>");
- out.println("</body>");
- out.println("</html>");
- }
- catch(Exception e){System.out.println(e);}
- }
- //Practical (Train Application InterThread Communication)
- platform.java File
- public class platform {
- boolean order = false;
- synchronized void green(){
- try{
- if(order){
- order = false;
- System.out.println("Train left the Platform");
- notify();
- }
- else{
- System.out.println("Train Coming,Platform Signal (Green)");
- wait();
- }
- }catch(Exception e){}
- }
- synchronized void red(){
- try{
- if(!order){
- order = true;
- System.out.println("Train on Platform,Platform Signal (Red)");
- notify();
- }
- else{
- System.out.println("Train still on Platform");
- wait();
- }
- }catch(Exception e){}
- }
- }
- train1.java File
- public class train1 extends Thread {
- platform p;
- train1(platform b){
- p=b;
- }
- public void run(){
- for (int j=1;j<10;j++){
- p.red();
- }
- }
- }
- train2.java File
- public class train2 extends Thread {
- platform p;
- train2(platform a){
- p=a;
- }
- public void run(){
- for(int j=1;j<10;j++){
- p.green();
- }
- }
- }
- signal.java File
- public class signal {
- public static void main(String[]args){
- platform ob1 = new platform();
- train1 ob2 = new train1(ob1);
- train2 ob3 = new train2(ob1);
- ob3.start();
- ob2.start();
- }
- }
- //Practical Calculator (Web Application)
- Calculator.java jFrameform file
- String a = jTextField1.getText();
- String b = jTextField2.getText();
- float num1 = Float.parseFloat(a);
- float num2 = Float.parseFloat(b);
- float result = (num1+num2);
- jTextField3.setText(String.format("%.2f", result));
- //Practical Chat Application
- 1)Client Window
- #Client Send
- 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);}
- #Client Receive
- try{
- Socket s = new Socket("localhost",2000);
- DataInputStream is = new DataInputStream(s.getInputStream());
- String msg = is.readUTF();
- jTextField1.setText(msg);
- }catch(Exception e){System.out.println(e);}
- #Client Quit
- System.exit(0);
- 2)Server Window
- #Server Send
- 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);}
- #Server Receieve
- try{
- ServerSocket ss = new ServerSocket(2000);
- Socket s = ss.accept();
- DataInputStream is = new DataInputStream(s.getInputStream());
- String msg = is.readUTF();
- jTextField1.setText(msg);
- }catch(Exception e){System.out.println(e);}
- #Server Quit
- System.exit(0);
- //Practical Table jsp file
- 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="table.jsp">
- Enter Number : <input type="text" name="number">
- <input type="submit">
- </form>
- </body>
- </html>
- table.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 border="1">
- <%
- String n = request.getParameter("number");
- int num = Integer.parseInt(n);
- //Down
- for(int i=1;i<=10;i++){
- //Forward
- out.println("<tr>");
- for(int j=1;j<=num;j++){
- out.println("<td>" +(i*j)+ "</td>");
- }
- out.println("</tr>");
- }
- %>
- </table>
- </body>
- </html>
- //Practical (Train Platform)
- platform.java file
- public class platform {
- boolean order = false;
- synchronized void green(){
- try{
- if(order){
- order = false;
- System.out.println("Train left the Platform");
- notify();
- }
- else{
- System.out.println("Train Coming,Platform Signal (Green)");
- wait();
- }
- }catch(Exception e){}
- }
- synchronized void red(){
- try{
- if(!order){
- order = true;
- System.out.println("Train on Platform,Platform Signal (Red)");
- notify();
- }
- else{
- System.out.println("Train still on Platform");
- wait();
- }
- }catch(Exception e){}
- }
- }
- train1.java file
- public class train1 extends Thread {
- platform p;
- train1(platform b){
- p=b;
- }
- public void run(){
- for (int j=1;j<10;j++){
- p.red();
- }
- }
- }
- train2.java file
- public class train2 extends Thread {
- platform p;
- train2(platform a){
- p=a;
- }
- public void run(){
- for(int j=1;j<10;j++){
- p.green();
- }
- }
- }
- signal.java File
- public class signal {
- public static void main(String[]args){
- platform ob1 = new platform();
- train1 ob2 = new train1(ob1);
- train2 ob3 = new train2(ob1);
- ob3.start();
- ob2.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement