Advertisement
MK7265

Untitled

Mar 10th, 2023 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. // Practical (Traffic Complaint Website)
  2.  
  3. index.html page
  4.  
  5. <html>
  6. <head>
  7. <title>TODO supply a title</title>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. </head>
  11. <body>
  12. <form action="insert">
  13. Vehicle Number : <input type="text" name="vname">
  14. Complaint : <input type ="text" name="vcom">
  15. Name : <input type="text" name="naam">
  16. <input type="submit">
  17. </form>
  18. </body>
  19. </html>
  20.  
  21.  
  22. view.html page
  23.  
  24. <html>
  25. <head>
  26. <title>TODO supply a title</title>
  27. <meta charset="UTF-8">
  28. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  29. </head>
  30. <body>
  31. <form action="view">
  32. Enter Number : <input type="text" name="vnum">
  33. <input type="submit">
  34. </form>
  35. </body>
  36. </html>
  37.  
  38.  
  39. insert.java file
  40.  
  41. public class insert extends HttpServlet {
  42. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44. response.setContentType("text/html;charset=UTF-8");
  45. try (PrintWriter out = response.getWriter()) {
  46.  
  47. //Initializing Variable for Storing Data
  48. String vehicle = request.getParameter("vname");
  49. String com = request.getParameter("vcom");
  50. String naam = request.getParameter("naam");
  51.  
  52. //Connection of Database
  53. String dsn = "jdbc:derby://localhost:1527/Traffic";
  54. String user = "root";
  55. String pass = "root";
  56.  
  57. try{
  58. Connection c = DriverManager.getConnection(dsn, user, pass);
  59.  
  60. //Query
  61. String sql = "Insert into Vehicle values('"+vehicle+"','"+com+"','"+naam+"')";
  62. Statement s = c.createStatement();
  63. s.executeUpdate(sql);
  64. }catch(Exception e){
  65. System.out.println(e);
  66. }
  67.  
  68.  
  69. out.println("<!DOCTYPE html>");
  70. out.println("<html>");
  71. out.println("<head>");
  72. out.println("<title>Servlet insert</title>");
  73. out.println("</head>");
  74. out.println("<body>");
  75. out.println("<h1>Data Inserted</h1>");
  76. out.println("</body>");
  77. out.println("</html>");
  78. }
  79. }
  80.  
  81.  
  82. view.java File
  83.  
  84. public class view extends HttpServlet {
  85. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  86. throws ServletException, IOException {
  87. response.setContentType("text/html;charset=UTF-8");
  88. try (PrintWriter out = response.getWriter()) {
  89.  
  90. //Initialize Variable to store data
  91. String vehiclenum = request.getParameter("vnum");
  92.  
  93. //Conntecting to Database
  94. String dsn = "jdbc:derby://localhost:1527/Traffic";
  95. String user = "root";
  96. String pass = "root";
  97.  
  98. try{
  99. Connection c = DriverManager.getConnection(dsn,user,pass);
  100.  
  101. //Select Vehicle,Complaint,Name from Vehicle where Vehicle="vehiclenum"
  102. String sql = "Select Vehicle,Complaint,Name from Vehicle where Vehicle='"+vehiclenum+"'";
  103.  
  104. Statement s = c.createStatement();
  105. ResultSet rs = s.executeQuery(sql);
  106. rs.next();
  107.  
  108. String vnum = rs.getString("Vehicle");
  109. String comp = rs.getString("Complaint");
  110. String name = rs.getString("Name");
  111.  
  112. //Response
  113. out.println("<!DOCTYPE html>");
  114. out.println("<html>");
  115. out.println("<head>");
  116. out.println("<title>Servlet view</title>");
  117. out.println("</head>");
  118. out.println("<body>");
  119. out.println("<h2>"+vnum+ " "+comp+ " "+name+"</h2>");
  120. out.println("</body>");
  121. out.println("</html>");
  122. }
  123. catch(Exception e){System.out.println(e);}
  124. }
  125.  
  126.  
  127.  
  128. //Practical (Train Application InterThread Communication)
  129.  
  130. platform.java File
  131.  
  132. public class platform {
  133. boolean order = false;
  134. synchronized void green(){
  135. try{
  136. if(order){
  137. order = false;
  138. System.out.println("Train left the Platform");
  139. notify();
  140. }
  141. else{
  142. System.out.println("Train Coming,Platform Signal (Green)");
  143. wait();
  144. }
  145. }catch(Exception e){}
  146. }
  147. synchronized void red(){
  148. try{
  149. if(!order){
  150. order = true;
  151. System.out.println("Train on Platform,Platform Signal (Red)");
  152. notify();
  153. }
  154. else{
  155. System.out.println("Train still on Platform");
  156. wait();
  157. }
  158. }catch(Exception e){}
  159. }
  160. }
  161.  
  162. train1.java File
  163.  
  164. public class train1 extends Thread {
  165. platform p;
  166. train1(platform b){
  167. p=b;
  168. }
  169. public void run(){
  170. for (int j=1;j<10;j++){
  171. p.red();
  172. }
  173. }
  174. }
  175.  
  176.  
  177. train2.java File
  178.  
  179. public class train2 extends Thread {
  180. platform p;
  181. train2(platform a){
  182. p=a;
  183. }
  184. public void run(){
  185. for(int j=1;j<10;j++){
  186. p.green();
  187. }
  188. }
  189.  
  190. }
  191.  
  192.  
  193. signal.java File
  194.  
  195. public class signal {
  196. public static void main(String[]args){
  197. platform ob1 = new platform();
  198. train1 ob2 = new train1(ob1);
  199. train2 ob3 = new train2(ob1);
  200. ob3.start();
  201. ob2.start();
  202. }
  203. }
  204.  
  205.  
  206.  
  207. //Practical Calculator (Web Application)
  208.  
  209. Calculator.java jFrameform file
  210.  
  211. String a = jTextField1.getText();
  212. String b = jTextField2.getText();
  213.  
  214. float num1 = Float.parseFloat(a);
  215. float num2 = Float.parseFloat(b);
  216. float result = (num1+num2);
  217.  
  218. jTextField3.setText(String.format("%.2f", result));
  219.  
  220.  
  221.  
  222. //Practical Chat Application
  223.  
  224. 1)Client Window
  225.  
  226. #Client Send
  227.  
  228. try{
  229. Socket s = new Socket("localhost",2000);
  230. DataOutputStream os = new DataOutputStream(s.getOutputStream());
  231. String data = jTextField1.getText();
  232. os.writeUTF(data);
  233. }catch(Exception e){System.out.println(e);}
  234.  
  235. #Client Receive
  236.  
  237. try{
  238. Socket s = new Socket("localhost",2000);
  239. DataInputStream is = new DataInputStream(s.getInputStream());
  240. String msg = is.readUTF();
  241. jTextField1.setText(msg);
  242. }catch(Exception e){System.out.println(e);}
  243.  
  244.  
  245. #Client Quit
  246.  
  247. System.exit(0);
  248.  
  249. 2)Server Window
  250.  
  251. #Server Send
  252.  
  253. try{
  254. ServerSocket ss = new ServerSocket(2000);
  255. Socket s = ss.accept();
  256. DataOutputStream os = new DataOutputStream(s.getOutputStream());
  257. String data = jTextField1.getText();
  258. os.writeUTF(data);
  259. }catch(Exception e){System.out.println(e);}
  260.  
  261. #Server Receieve
  262.  
  263. try{
  264. ServerSocket ss = new ServerSocket(2000);
  265. Socket s = ss.accept();
  266. DataInputStream is = new DataInputStream(s.getInputStream());
  267. String msg = is.readUTF();
  268. jTextField1.setText(msg);
  269. }catch(Exception e){System.out.println(e);}
  270.  
  271. #Server Quit
  272.  
  273. System.exit(0);
  274.  
  275.  
  276. //Practical Table jsp file
  277.  
  278. index.html
  279.  
  280. <html>
  281. <head>
  282. <title>TODO supply a title</title>
  283. <meta charset="UTF-8">
  284. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  285. </head>
  286. <body>
  287. <form action="table.jsp">
  288. Enter Number : <input type="text" name="number">
  289. <input type="submit">
  290. </form>
  291. </body>
  292. </html>
  293.  
  294.  
  295. table.jsp
  296.  
  297. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  298. <!DOCTYPE html>
  299. <html>
  300. <head>
  301. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  302. <title>JSP Page</title>
  303. </head>
  304. <body>
  305. <table border="1">
  306.  
  307. <%
  308. String n = request.getParameter("number");
  309. int num = Integer.parseInt(n);
  310.  
  311. //Down
  312. for(int i=1;i<=10;i++){
  313. //Forward
  314. out.println("<tr>");
  315. for(int j=1;j<=num;j++){
  316. out.println("<td>" +(i*j)+ "</td>");
  317. }
  318. out.println("</tr>");
  319. }
  320. %>
  321.  
  322. </table>
  323. </body>
  324. </html>
  325.  
  326.  
  327. //Practical (Train Platform)
  328.  
  329. platform.java file
  330.  
  331. public class platform {
  332. boolean order = false;
  333. synchronized void green(){
  334. try{
  335. if(order){
  336. order = false;
  337. System.out.println("Train left the Platform");
  338. notify();
  339. }
  340. else{
  341. System.out.println("Train Coming,Platform Signal (Green)");
  342. wait();
  343. }
  344. }catch(Exception e){}
  345. }
  346. synchronized void red(){
  347. try{
  348. if(!order){
  349. order = true;
  350. System.out.println("Train on Platform,Platform Signal (Red)");
  351. notify();
  352. }
  353. else{
  354. System.out.println("Train still on Platform");
  355. wait();
  356. }
  357. }catch(Exception e){}
  358. }
  359. }
  360.  
  361.  
  362. train1.java file
  363.  
  364. public class train1 extends Thread {
  365. platform p;
  366. train1(platform b){
  367. p=b;
  368. }
  369. public void run(){
  370. for (int j=1;j<10;j++){
  371. p.red();
  372. }
  373. }
  374. }
  375.  
  376.  
  377. train2.java file
  378.  
  379. public class train2 extends Thread {
  380. platform p;
  381. train2(platform a){
  382. p=a;
  383. }
  384. public void run(){
  385. for(int j=1;j<10;j++){
  386. p.green();
  387. }
  388. }
  389.  
  390. }
  391.  
  392. signal.java File
  393.  
  394. public class signal {
  395. public static void main(String[]args){
  396. platform ob1 = new platform();
  397. train1 ob2 = new train1(ob1);
  398. train2 ob3 = new train2(ob1);
  399. ob3.start();
  400. ob2.start();
  401. }
  402. }
  403.  
  404.  
  405.  
  406.  
  407.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement