Advertisement
MK7265

Untitled

Mar 10th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. //Practical (Traffic Website)
  2.  
  3. Index.html
  4. <html>
  5. <head>
  6. <title>TODO supply a title</title>
  7. <meta charset="UTF-8">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. </head>
  10. <body>
  11. <form action="insert">
  12. Vehicle Number : <input type="text" name="vname">
  13. Complaint : <input type ="text" name="vcom">
  14. Name : <input type="text" name="naam">
  15. <input type="submit">
  16. </form>
  17. </body>
  18. </html>
  19.  
  20. insert.java (Servlet):
  21. import java.sql.*;
  22. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  23. throws ServletException, IOException {
  24. response.setContentType("text/html;charset=UTF-8");
  25. try (PrintWriter out = response.getWriter()) {
  26. //Step 1
  27. String vehicle = request.getParameter("vname");
  28. String com = request.getParameter("vcom");
  29. String naam = request.getParameter("naam");
  30.  
  31. //Step 2
  32. String dsn = "jdbc:derby://localhost:1527/Traffic";
  33. String user = "root";
  34. String pass = "root";
  35.  
  36. try{
  37. Connection c = DriverManager.getConnection(dsn,user,pass);
  38.  
  39. //Insert Into Vehicle Values('vid','com','naam')
  40. String sql = "Insert into Vehicle Values('" +vehicle+ "','" +com+ "','" +naam+ "')";
  41.  
  42. Statement s = c.createStatement();
  43. s.executeUpdate(sql);
  44.  
  45. }catch(Exception e){
  46. System.out.println(e);
  47. }
  48.  
  49. out.println("<!DOCTYPE html>");
  50. out.println("<html>");
  51. out.println("<head>");
  52. out.println("<title>Servlet insert</title>");
  53. out.println("</head>");
  54. out.println("<body>");
  55. out.println("<h1>Servlet insert at Data INserted</h1>");
  56. out.println("</body>");
  57. out.println("</html>");
  58. }
  59. }
  60. Check Data Inserted or not in Service  dsn  Tables  Table_NameView Data
  61.  
  62.  
  63.  
  64.  
  65. View.html :
  66. <html>
  67. <head>
  68. <title>TODO supply a title</title>
  69. <meta charset="UTF-8">
  70. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  71. </head>
  72. <body>
  73. <form action="viewData.jsp">
  74. ENter Number : <input type="text" name="vnum">
  75. <input type="submit">
  76. </form>
  77. </body>
  78. </html>
  79.  
  80. viewData.java (Servlet):
  81. import java.sql.*;
  82. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  83. throws ServletException, IOException {
  84. response.setContentType("text/html;charset=UTF-8");
  85. try (PrintWriter out = response.getWriter()) {
  86. //Step 1
  87. String vid = request.getParameter("vnum");
  88.  
  89. //Step 2
  90. String dsn = "jdbc:derby://localhost:1527/Traffic";
  91. String user = "root";
  92. String pass = "root";
  93.  
  94. try{
  95. Connection c = DriverManager.getConnection(dsn,user,pass);
  96.  
  97. //Select Vid,Complaint,Name From Vehicle where vid = 'vid'
  98. String sql = "Select Vid,Complaint,Name From Vehicle where Vid = '" +vid+ "'";
  99.  
  100. Statement s = c.createStatement();
  101. ResultSet rs = s.executeQuery(sql);
  102.  
  103. rs.next();
  104.  
  105. String vnum = rs.getString("VID");
  106. String comp = rs.getString("Complaint");
  107. String name= rs.getString("Name");
  108.  
  109.  
  110. //Response
  111. out.println("<!DOCTYPE html>");
  112. out.println("<html>");
  113. out.println("<head>");
  114. out.println("<title>Servlet viewData</title>");
  115. out.println("</head>");
  116. out.println("<body>");
  117. out.println(vnum+ " " +comp+ " " +name);
  118. out.println("</body>");
  119. out.println("</html>");
  120.  
  121. }catch(Exception e){
  122. System.out.println(e);
  123.  
  124. }}}
  125.  
  126.  
  127. //Practical (Table using jsp)
  128.  
  129. Index.html :
  130. <html>
  131. <head>
  132. <title>TODO supply a title</title>
  133. <meta charset="UTF-8">
  134. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  135. </head>
  136. <body>
  137. <form action="newjsp.jsp">
  138. ENter Number : <input type="text" name="vnum">
  139. <input type="submit">
  140. </form>
  141. </body>
  142. </html>
  143.  
  144. newjsp.jsp (JSP):
  145. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  146. <!DOCTYPE html>
  147. <html>
  148. <head>
  149. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  150. <title>JSP Page</title>
  151. </head>
  152. <body>
  153. <table>
  154.  
  155. <%
  156. String n = request.getParameter("vnum");
  157. int num = Integer.parseInt(n);
  158.  
  159. //Niche Jaane ki liye
  160. for(int i=1;i<=10;i++){
  161. //Agge Baadhne ke liye
  162. out.println("<tr>");
  163. for(int j=1;j<=num;j++){
  164. out.println("<td>" +(i*j)+ "</td>");
  165. }
  166. out.println("</tr>");
  167. }
  168. %>
  169.  
  170. </table>
  171. </body>
  172. </html>
  173.  
  174. //Practical (Chat )
  175.  
  176. Client jFrame banake..
  177. Client.java
  178. import java.net.*;
  179. import java.io.*;
  180.  
  181. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  182. try{
  183. Socket s = new Socket("localhost",2000);
  184.  
  185. DataOutputStream os = new DataOutputStream(s.getOutputStream());
  186.  
  187. String data = jTextField1.getText();
  188.  
  189. os.writeUTF(data);
  190.  
  191. }catch(Exception e){
  192. System.out.println(e);
  193. }
  194. }
  195.  
  196. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  197. try{
  198. Socket s = new Socket("localhost",2000);
  199.  
  200. DataInputStream is = new DataInputStream(s.getInputStream());
  201.  
  202. String msg = is.readUTF();
  203. System.out.println(msg);
  204. }catch(Exception e){
  205. System.out.println(e);
  206. }
  207. }
  208.  
  209. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  210. System.exit(0);
  211.  
  212. }
  213.  
  214. Create a new Project for Server…..
  215. Server > jFrame banake
  216. Server.java
  217. import java.net.*;
  218. import java.io.*;
  219.  
  220. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  221. try{
  222. ServerSocket ss = new ServerSocket(2000);
  223.  
  224. Socket s = ss.accept();
  225.  
  226. DataOutputStream os = new DataOutputStream(s.getOutputStream());
  227.  
  228. String data = jTextField1.getText();
  229.  
  230. os.writeUTF(data);
  231. }catch(Exception e){
  232. System.out.println(e);
  233. }
  234. }
  235.  
  236. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  237. try{
  238. ServerSocket ss = new ServerSocket(2000);
  239.  
  240. Socket s = ss.accept();
  241.  
  242. DataInputStream is = new DataInputStream(s.getInputStream());
  243.  
  244. String msg = is.readUTF();
  245. System.out.println(msg);
  246.  
  247. }catch(Exception e){
  248. System.out.println(e);
  249. }
  250. }
  251.  
  252. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  253. System.exit(0);
  254. }
  255.  
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement