342 Advanced Internet Programming (v) Email Email address must contain at least an @ sign and a dot(.) When all input are entered correctly CU IDOL SELF LEARNING MATERIAL (SLM)
PRACTICAL JAVA SERVER PAGES UNIT 3 vcvc Practical - 1 Create a Bean that Represents Information Needed to Calculate an Employee's Salary. Has String (Employee Name) and int (Employee ID) Properties. Create an Application to Demonstrate Automatically Filling in Bean Properties from Request Parameters 1. Create new Web Application File> New Project > Under Categories select “Java Web” – Under Project Select Web Application > Next > Rename Project as “JSPBeanEx” > Next > Next > Finish. 2. In index.htmlw <body> <div>XYZ org</div> <form action =\"NewServlet\"> Employee Name: <input type =\"text\" name=\"empname\"/> Employee ID: <input type =\"text\" name=\"empid\"/> <input type=\"submit\" value=\"submit\"/> </form> </body> 3. Creating Java bean CU IDOL SELF LEARNING MATERIAL (SLM)
344 Advanced Internet Programming Type package name as “pkg”> Finish. Add new Java Class “EmpDetails” in the Package “pkg” //EmpDetails.java package pkg; public class EmpDetails { private String empname; private int empid; public int getEmpid() { return empid; } public void setEmpid(int empid) { this.empid = empid; } public String getEmpname() { CU IDOL SELF LEARNING MATERIAL (SLM)
Java Server Pages 345 return empname; } public void setEmpname(String empname) { this.empname = empname; }} 4. Create a servlet “NewServlet” to set EmpDetails bean proerties. Then click Next. //NewServlet.java import javax.servlet.RequestDispatcher; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\"text/html;charset=UTF-8\"); try (PrintWriter out = response.getWriter()) { pkg.EmpDetails emp=new pkg.EmpDetails(); int empid=Integer.parseInt(request.getParameter(\"empid\")); String empname=request.getParameter(\"empname\"); emp.setEmpid(empid); CU IDOL SELF LEARNING MATERIAL (SLM)
346 Advanced Internet Programming emp.setEmpname(empname); /**** Storing Bean In Session ****/ request.getSession().setAttribute(\"emp\", emp); RequestDispatcher rd = request.getRequestDispatcher(\"/beanData.jsp\"); rd.forward(request, response); } 5. Create jsp “beanData” to show the bean properties. CU IDOL SELF LEARNING MATERIAL (SLM)
Java Server Pages 347 //beanData,jsp <%@page language=\"java\" contentType=\"text/html\" pageEncoding=\"UTF-8\"%> <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> <title>Emp Details</title> </head> <body> Employee Id:${emp.empid}<br> Name:${emp.empname} </body> </html> 6. Executing the application: (i) Right Click on application to build. (ii) Right Click on application to build. (iii) Right Click on application to execute(Select Run). CU IDOL SELF LEARNING MATERIAL (SLM)
348 Advanced Internet Programming Experiment: II (i) Create an emp table in the database with fields name, id and designation. (ii) Create a HTML file with fields name, id and designation. Make sure that the textfields have the same name as name, id and designation. (iii) Create a javabean with fields name, id and designation. (iv) On click of the submit button of the HTML, invoke a jsp page which will extract the values that were given by the HTML page and it invokes a servlet. (v) The Servlet will make a connection to the database and store the value in the table. (vi) Connection to the database should have been established using a separate java class. (vii) You can enhance the program by including more features like deletion, updation and selecting all records. 1. Open MySql Command Line Client create database mscit; use mscit; create table emp(id int, name varchar(20), designation varchar(20)); select * from emp; 2. Create new Web Application File> New Project > Under Categories select “Java Web” – Under Project Select Web Application > Next > Rename Project as “JSPBeanEx2” > Next > Next > Finish. 3. In index.html <body> <div>XYZ org</div> <form action =\"newjsp.jsp\"> Employee Name: <input type =\"text\" name=\"name\"/> Employee ID: <input type =\"text\" name=\"id\"/> CU IDOL SELF LEARNING MATERIAL (SLM)
Java Server Pages 349 Employee Designation: <input type =\"text\" name=\"designation\"/> <input type=\"submit\" name=\"save\" value=\"Save\"/> </br> </form> <form action =\"ShowData\"> <input type=\"submit\" name=\"show\" value=\"Show\"/> </form> </body> 4. Creating a Java bean Type package name as “pkg”> Finish. Add new Java Class “EmpDetails” in the Package “pkg” CU IDOL SELF LEARNING MATERIAL (SLM)
350 Advanced Internet Programming //EmpDetails.java package pkg; public class EmpDetails { private String empname; private int empid; private String designation; public int getEmpid() { return empid; } public void setEmpid(int empid) { this.empid = empid; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public String getEmpDesignation() { return designation; } public void setEmpDesignation(String designation) { this.designation = designation; }} CU IDOL SELF LEARNING MATERIAL (SLM)
Java Server Pages 351 5. Creating jsp “newjsp” to set bean properties //newjsp.jsp <%@page language=\"java\" 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> <% pkg.EmpDetails emp=new pkg.EmpDetails(); int id=Integer.parseInt(request.getParameter(\"id\")); String name=request.getParameter(\"name\"); CU IDOL SELF LEARNING MATERIAL (SLM)
352 Advanced Internet Programming String designation=request.getParameter(\"designation\"); emp.setEmpid(id); emp.setEmpname(name); emp.setEmpDesignation(designation); /**** Storing Bean In Session ****/ request.getSession().setAttribute(\"emp\", emp); RequestDispatcher rd = request.getRequestDispatcher(\"/NewServlet\"); rd.forward(request, response); %> </body> </html> 6. Create a Servlet “NewServlet” to store values in database. Then click Next. CU IDOL SELF LEARNING MATERIAL (SLM)
Java Server Pages 353 //NewServlet.java import java.sql.*; import pkg.EmpDetails; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\"text/html;charset=UTF-8\"); try (PrintWriter out = response.getWriter()) { Connection dbcon = null; PreparedStatement stmt = null; try { if(request.getParameter(\"save\").equals(\"Save\")) { Class.forName(\"com.mysql.jdbc.Driver\").newInstance(); dbcon = DriverManager.getConnection(\"jdbc:mysql://localhost/mscit\",\"root\",\"123456\"); stmt=dbcon.prepareStatement(\"insert into Emp values(?,?,?)\"); EmpDetails emp = new EmpDetails(); emp=(EmpDetails)request.getSession().getAttribute(\"emp\"); stmt.setInt(1,emp.getEmpid()); stmt.setString(2,emp.getEmpname()); stmt.setString(3,emp.getEmpDesignation()); stmt.executeUpdate(); out.println(\" records inserted\"); dbcon.close(); }} catch(Exception e) { out.println(e.toString()); }} catch(Exception e) { }} Add mysql java connector jar file CU IDOL SELF LEARNING MATERIAL (SLM)
354 Advanced Internet Programming Add Servlet “ShowData” to display values //ShowData.java 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()) { Connection dbcon = null; PreparedStatement stmt = null; Class.forName(\"com.mysql.jdbc.Driver\").newInstance(); dbcon = DriverManager.getConnection(\"jdbc:mysql://localhost/mscit\",\"root\",\"123456\"); String query = \"SELECT * FROM emp\"; Statement st = dbcon.createStatement(); CU IDOL SELF LEARNING MATERIAL (SLM)
Java Server Pages 355 ResultSet rs = st.executeQuery(query); out.println(\"<!DOCTYPE html>\"); out.println(\"<html>\"); out.println(\"<head>\"); out.println(\"<title>Servlet ShowData</title>\"); out.println(\"</head>\"); out.println(\"<body>\"); out.println(\"ID Name Designation\"); out.println(\"<br>\"); while (rs.next()) { int id = rs.getInt(\"id\"); String name = rs.getString(\"name\"); String designation = rs.getString(\"designation\"); out.println(id+\" \"+name+\" \"+designation); out.println(\"<br>\"); } out.println(\"</body>\"); out.println(\"</html>\"); } catch(Exception e) { } } 7. Executing the application: (i) Right Click on application to build. CU IDOL SELF LEARNING MATERIAL (SLM)
356 Advanced Internet Programming (ii) Right Click on application to build. (iii) Right Click on application to execute(Select Run). Note: Delete and Update can be implemented in the same way. CU IDOL SELF LEARNING MATERIAL (SLM)
PRACTICAL AJAX UNIT 4 vcvc Practical - 1 (A) Develop a web application to dynamically check for availability of an email-id for registration. If email-id does not exist in the database “Available!” else “Not Available!”, should be displayed next to the email textbox. If email-id pattern does not conform to an email-id, display Invalid Email Id. Perform the check when the textbox loses focus. Consider the following fields for the Profile table: EMAIL, PASSWORD, NAME, DATEOFBIRTH, GENDER, CITY, PINCODE, MOBILE Note* : Check should be performed irrespective of uppercase or lowercase data. 1. Open MySql Command Line Client create database mscit; use mscit; create table emp(id int, name varchar(20), email varchar(30),pass varchar(20),dob varchar(20), gender varchar(6),city varchar(20), pincode varchar(10), mobile varchar(10)); insert into emp values(2,'mscit','[email protected]','123456','1-10-1994', 'male','mumbai','400000','9865432222'); insert into emp values(1,'mscit','[email protected]','123456','1-10-1994', 'male','mumbai','400000','9876543210'); select email from emp; CU IDOL SELF LEARNING MATERIAL (SLM)
358 Advanced Internet Programming 2. Create new Web Application File> New Project > Under Categories select “Java Web” – Under Project Select Web Application > Next > Rename Project as “JavaAjaxTest” > Next > Next > Finish. 3. In index.html <!doctype html> <html> <body > <form method='post' action=\"saveServlet\"> ID<input type='text' name='id' /><br> Name:<input type='text' name='name' /><br> Email: <input type='text' name='email' id='email'> <span id='response' style=\"color:red\"></span><br> Password: <input type='password' name='pass' /><br> DOB:<input type='text' name='dob' /><br> Gender<input type='radio' name='gender' value=\"Male\">Male <input type='radio' name='gender' value=\"Female\">Female<br> CITY:<input type='text' name='city' /><br> Pincode:<input type='text' name='pincode' /><br> Mobile:<input type='text' name='mobile' /><br> <input type='submit' value='submit' name='submit'><br> </form> <!-- Script --> <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script> <script> $(document).ready(function(){ $('#email').focusout(function(){ var email = $('#email').val(); $.ajax({ type: 'post', data: {ajax: 1,email:email}, url:'checkEmail', success: function(response){ $('#response').text( response); } }); }); }); </script> </body> </html> CU IDOL SELF LEARNING MATERIAL (SLM)
AJAX 359 4. Add a servlet checkEmail //checkEmail.java protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\"text/html;charset=UTF-8\"); try (PrintWriter out = response.getWriter()) { Connection dbcon = null; Class.forName(\"com.mysql.jdbc.Driver\").newInstance(); dbcon = DriverManager.getConnection(\"jdbc:mysql://localhost/mscit\",\"root\",\"123456\"); String query = \"SELECT email FROM emp\"; Statement st = dbcon.createStatement(); ResultSet rs = st.executeQuery(query); String email=request.getParameter(\"email\"); int existEmail=0; while (rs.next()) { String dbemail = rs.getString(\"email\"); if(email.equalsIgnoreCase(dbemail)) { existEmail=1; }} if(existEmail==0) out.println(\"Available\"); else out.println(\"Not-Available\"); } CU IDOL SELF LEARNING MATERIAL (SLM)
360 Advanced Internet Programming catch(Exception e) { System.out.println(\"error\"+e.getMessage()); } } Add new Servlet “saveServlet” to save data in database //saveServlet.java protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\"text/html;charset=UTF-8\"); PrintWriter out = response.getWriter(); try { Connection dbcon = null; PreparedStatement stmt = null; Class.forName(\"com.mysql.jdbc.Driver\").newInstance(); dbcon = DriverManager.getConnection(\"jdbc:mysql://localhost/mscit\",\"root\",\"123456\"); stmt=dbcon.prepareStatement(\"insert into Emp values(?,?,?,?,?,?,?,?,?)\"); stmt.setInt(1,Integer.parseInt(request.getParameter(\"id\"))); stmt.setString(2,request.getParameter(\"name\")); stmt.setString(3,request.getParameter(\"email\")); stmt.setString(4,request.getParameter(\"pass\")); stmt.setString(5,request.getParameter(\"dob\")); stmt.setString(6,request.getParameter(\"gender\")); stmt.setString(7,request.getParameter(\"city\")); stmt.setString(8, request.getParameter(\"pincode\")); stmt.setString(9,request.getParameter(\"mobile\")); stmt.executeUpdate(); out.println(\" records inserted\"); dbcon.close(); out.println(\"<!DOCTYPE html>\"); out.println(\"<html>\"); out.println(\"<head>\"); out.println(\"<title>Servlet saveServlet</title>\"); out.println(\"</head>\"); out.println(\"<body>\"); out.println(\"<h1>Data inserted\"); out.println(\"</body>\"); out.println(\"</html>\"); } CU IDOL SELF LEARNING MATERIAL (SLM)
AJAX 361 catch(Exception e) { out.println(e.getMessage()); }} Add mysql java connector jar file 5. Executing the application: (i) Right Click on application to build. CU IDOL SELF LEARNING MATERIAL (SLM)
362 Advanced Internet Programming (ii) Right Click on application to build. (iii) Right Click on application to execute(Select Run). CU IDOL SELF LEARNING MATERIAL (SLM)
www.cuidol.in 1800-1213-88800 INSTITUTE OF DISTANCE & ONLINE LEARNING NH-95, Chandigarh-Ludhiana Highway, Gharuan, Mohali (Punjab) Phone:- 7527009635 | Email: [email protected] FOLLOW US ON:
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372