Guest User

Untitled

a guest
Oct 21st, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.98 KB | None | 0 0
  1. Create a servlet for a login page. If the username and password are correct then it says message “Hello ” else a message “login failed”
  2. Index.html
  3. <html><head><title>Login Form</title></head>
  4. <form action="LoginServlet" >
  5. Enter User ID<input type="text" name="txtId"><br>
  6. Enter Password<input type="password" name="txtPass"><br>
  7. <input type="reset"><input type="submit" value=" Click to Login " ></form></html>
  8. LoginServlet.java
  9. import java.io.*;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.*;
  12. public class LoginServlet extends HttpServlet {
  13. public void doGet(HttpServletRequest request, HttpServletResponse response)
  14. throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
  15. out.println("<html><head><title>Servlet LoginServlet</title></head>"); String uname = request.getParameter("txtId");
  16. String upass = request.getParameter("txtPass"); if(uname.equals("sid") && upass.equals("12345")){ out.println("<body bgcolor=blue >"); out.println("<h1> Welcome !!! "+uname+"</h1>"); }
  17. else{
  18. out.println("<body bgcolor=red >");
  19. out.println("<h1> Login Fail !!! </h1>");}
  20. out.println("</body></html>");}}
  21. 2.Create a JSP application to demonstrate the use of JSTL. (add jstl-1.2 and go to web-inf add –other-web.xml)
  22. Index.jsp
  23. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  24. <!DOCTYPE html>
  25. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  26. <title>Index Page</title> </head> <form action="jstlDemo.jsp" method="post">
  27. first name :<input type="text" name="fname"/><br/>
  28. last name :<input type="text" name="lname"/><br/>
  29. <input type="submit" value="check jstl Demo"/>
  30. </form> <body> <h1>Hello World!</h1> </body></html>
  31. jstlDemo.jsp
  32. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  33. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  34. <!DOCTYPE html>
  35. <html>
  36. <head>
  37. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  38. <title>JSP Page</title>
  39. </head>
  40. <body>
  41. first name : <b> <c:out value="${param.fname}"> </c:out> </b> <br/>
  42. last name : <b> <c:out value="${param.lname}"> </c:out> </b> <br/> <br/>
  43. use of if statement <br> <c:set var="mycount" value="25"/>
  44. <b> <c:out value="your count is 25"/> </b> <br> <br>
  45. use of for each statment <br>
  46. <c:forEach var="count" begin="101" end="105">
  47. <b> <c:out value="${count}"/> </b> </c:forEach> <br> <br> Exception catching example
  48. <p> <c:catch var="myException"> <%int number=10/0;%> </c:catch>
  49. <b> the Exception is:${myException} </b> </p> </body> </html>
  50. 3.Develop a simple JSP application to display values obtained from the use of implicit objects of various types
  51. Index.jsp (glass fish server and normal web applictaion)
  52. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  53. <html><head><title>JSP Page</title></head><body>
  54. <h1>Use of Intrinsic Objects in JSP</h1>
  55. <h1>Request Object </h1>
  56. Query String <%=request.getQueryString() %><br>
  57. Context Path <%=request.getContextPath() %><br>
  58. Remote Host <%=request.getRemoteHost() %><br>
  59. <h1>Response Object </h1>
  60. Character Encoding Type <%=response.getCharacterEncoding() %><br>
  61. Content Type<%=response.getContentType() %><br> Locale <%=response.getLocale() %><br>
  62. <h1>Session Object </h1>
  63. ID <%=session.getId() %><br>
  64. Creation Time <%=new java.util.Date(session.getCreationTime()) %><br>
  65. Last Access Time<%=new java.util.Date(session.getLastAccessedTime()) %><br></body>
  66. </html>
  67.  
  68. 4.Develop simple application to demonstrate accessing Session Bean using EJB
  69.  
  70.  
  71. 5.Write a servlet code to store the biodata details of a user into the database. (Name, age, address, hobbies, gender, Qualification).
  72. Index.html
  73. <html><head><title>Registration Page</title></head>
  74.  
  75. <body>
  76.  
  77.  
  78.  
  79.  
  80. <form action="RegisterServlet" >
  81.  
  82.  
  83. <H1>Welcome to Registration page</H1>
  84.  
  85.  
  86. Enter User Name
  87. <input type="text" name="txtUid"><br>
  88.  
  89. Enter Password
  90.  
  91. <input type="password" name="txtPass"><br>
  92.  
  93. Enter Email
  94. <input type="text" name="txtEmail" ><br>
  95.  
  96. Enter Country
  97.  
  98. <input type="text" name="txtCon" ><br>
  99.  
  100. <input type="reset" ><input type="submit" value="REGISTER" >
  101.  
  102. </form>
  103.  
  104.  
  105.  
  106.  
  107. </body> </html>
  108.  
  109.  
  110.  
  111.  
  112.  
  113. RegisterServlet.java
  114. import java.io.*;
  115. import java.sql.*;
  116. import javax.servlet.*;
  117. import javax.servlet.http.*;
  118. public class RegisterServlet extends HttpServlet {
  119. public void doGet(HttpServletRequest request, HttpServletResponse response)
  120. throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String id = request.getParameter("txtUid");
  121. String ps = request.getParameter("txtPass");
  122. String em = request.getParameter("txtEmail");
  123. String co = request.getParameter("txtCon");
  124. try{
  125. Class.forName("com.mysql.jdbc.Driver");
  126. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb","root","root");
  127. PreparedStatement pst = con.prepareStatement("insert into user values(?,?,?,?)");
  128. pst.setString(1,id);
  129. pst.setString(2,ps);
  130. pst.setString(3,em);
  131. pst.setString(4,co);
  132. int row = pst.executeUpdate(); out.println("<h1>"+row+ " Inserted Succesfullyyyyy"); }catch(Exception e){out.println(e);}}}
  133. 6. Develop simple EJB application to demonstrate Servlet Hit count using Singleton Session Beans.
  134. CounterBean.java (package name=counter.ejb)
  135. package counter.ejb;
  136. import javax.ejb.Singleton;
  137. @Singleton
  138. public class CounterBean {
  139. private int hits = 1;
  140. public int getHits() {
  141. return hits++; } }
  142. Count.java(package name=mypack)
  143. package mypack;
  144. import java.io.Serializable;
  145. import javax.ejb.EJB;
  146. import javax.enterprise.context.ConversationScoped;
  147. import javax.inject.Named;
  148. import counter.ejb.CounterBean;
  149. @Named("count")
  150. @ConversationScoped
  151. public class Count implements Serializable {
  152. @EJB
  153. private CounterBean counterBean;
  154. private int hitCount;
  155. public Count() {
  156. this.hitCount = 0;}
  157. public int getHitCount() {
  158. hitCount = counterBean.getHits();
  159. return hitCount; }
  160. public void setHitCount(int newHits) {
  161. this.hitCount = newHits; } }
  162.  
  163. 7.Develop a JSP application to authenticate user login with database.
  164.  
  165. 8.Create a JSP page to demonstrate the use of Expression language.
  166. 9.Create a Servlet application to upload and download a file.
  167. Index.html
  168. <html>
  169. <body>
  170. <form action="FileUploadServlet" method="post" enctype="multipart/form-data"> Select File to Upload:<input type="file" name="file" id="file">
  171. Destination <input type="text" value="/tmp" name="destination"><br>
  172. <input type="submit" value="Upload file" name="upload" id="upload">
  173. </form> </body> </html>
  174.  
  175. FileUploadServlet.java
  176. import java.io.*;
  177. import javax.servlet.*;
  178. import javax.servlet.annotation.MultipartConfig;
  179. import javax.servlet.http.*;
  180. @MultipartConfig
  181. public class FileUploadServlet extends HttpServlet {
  182. public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,
  183. IOException
  184. {res.setContentType("text/html");
  185. PrintWriter out = res.getWriter();
  186. String path=req.getParameter("destination");
  187. Part filePart=req.getPart("file");
  188. String filename=filePart.getSubmittedFileName().toString();
  189. out.print("<br><br><hr> file name: "+filename);
  190. OutputStream os=null;
  191. InputStream is=null;
  192. try { os=new FileOutputStream(new File(path+File.separator+filename));
  193. is=filePart.getInputStream();
  194. int read=0;
  195. while ((read = is.read()) != -1) {
  196. os.write(read);
  197. } out.println("<br>file uploaded sucessfully...!!!"); }
  198. catch(FileNotFoundException e){out.print(e); } } }
  199.  
  200. index.html
  201. <html> <head> <title>File Download Page</title> </head> <body>
  202. <h1>File Download Application</h1>
  203. Click <a href="DownloadServlet?filename=SampleChapter.pdf">Sample Chapter</a><br/><br/>
  204. Click <a href="DownloadServlet?filename=TOC.pdf">Table Of Contents</a></body> </html>
  205.  
  206.  
  207. DownloadServlet.java
  208. import java.io.IOException;
  209. import java.io.InputStream;
  210. import java.io.PrintWriter;
  211. import javax.servlet.ServletContext;
  212. import javax.servlet.ServletException;
  213. import javax.servlet.ServletOutputStream;
  214. import javax.servlet.http.HttpServlet;
  215. import javax.servlet.http.HttpServletRequest;
  216. import javax.servlet.http.HttpServletResponse;
  217. public class DownloadServlet extends HttpServlet {
  218. public void doGet(HttpServletRequest request, HttpServletResponse response)
  219. throws ServletException, IOException { response.setContentType("APPLICATION/OCTET-STREAM");
  220. String filename = request.getParameter("filename");
  221. ServletContext context = getServletContext();
  222. InputStream is = context.getResourceAsStream("/" + filename);
  223. PrintWriter out=response.getWriter();
  224. response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
  225. int i;
  226. while ((i=is.read()) != -1) {
  227. out.write(i);}
  228. is.close();out.close(); } }
  229.  
  230.  
  231. 10.Create an html page with fields, eno, name, age, desg, salary. Now on submit this data to a JSP page which will update the employee table of database with matching eno.
  232.  
  233.  
  234. 11.Develop servlet application to demonstrate basic calculator.
  235. index.html
  236. <html><head><title>Calculator App</title></head><body>
  237. <formaction="CalculatorServlet" >
  238. Enter First Number
  239. <input type="text"name="txtN1"><br>
  240. Enter Second Number
  241. <input type="text" name="txtN2" ><br> Select an Operation
  242.  
  243. <input type="radio" name="opr" value="+">ADDTION
  244. <input type="radio" name="opr" value="-">SUBSTRACTION <input type="radio" name="opr" value="*">MULTIPLY
  245. <input type="radio" name="opr" value="/">DIVIDE <br>
  246. <input type="reset">
  247. <input type="submit" value="Calculate" >
  248. </form></body></html>
  249. CalculatorServlet.java
  250. import java.io.IOException;
  251. import java.io.PrintWriter;
  252. import javax.servlet.ServletException;
  253. import javax.servlet.http.HttpServlet;
  254. import javax.servlet.http.HttpServletRequest;
  255. import javax.servlet.http.HttpServletResponse;
  256. public class CalculatorServlet extends HttpServlet {
  257. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  258. throws ServletException, IOException {
  259. response.setContentType("text/html;charset=UTF-8");
  260. PrintWriter out = response.getWriter(); {
  261. out.println("<html><head><title>Servlet CalculatorServlet</title></head><body>"); double n1 = Double.parseDouble(request.getParameter("txtN1"));
  262. double n2 = Double.parseDouble(request.getParameter("txtN2"));
  263. double result =0;
  264. String opr=request.getParameter("opr");
  265. if(opr.equals("+")) result=n1+n2;
  266. if(opr.equals("-")) result=n1-n2;
  267. if(opr.equals("*")) result=n1*n2;
  268. if(opr.equals("/")) result=n1/n2;
  269. out.println("<h1> Result = "+result);
  270. out.println("</body></html>"); } } }
  271. 12.Using Request Dispatcher Interface create a Servlet which will validate the password entered by the user, if the user has entered "Servlet" as password, then he will be forwarded to Welcome Servlet else the user will stay on the index.html page and an error message will be displayed.
  272. index.html
  273.  
  274. <html><head><title>Login Form</title></head>
  275.  
  276. <form action="LoginServlet" >
  277.  
  278. Enter User ID<input type="text" name="txtId"><br>
  279.  
  280. Enter Password<input type="password" name="txtPass"><br>
  281.  
  282. <input type="reset"><input type="submit" value=" Click to Login " ></form></html>
  283. LoginServlet.java
  284. import java.io.IOException;
  285. import java.io.PrintWriter;
  286. import javax.servlet.ServletException;
  287. import javax.servlet.http.HttpServlet;
  288. import javax.servlet.http.HttpServletRequest;
  289. import javax.servlet.http.HttpServletResponse;
  290. import javax.servlet.RequestDispatcher;
  291. public class LoginServlet extends HttpServlet {
  292. public void doGet(HttpServletRequest request, HttpServletResponse response)
  293. throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
  294. out.println("<html><head>");
  295. out.println("<title>Servlet LoginServlet</title></head>");
  296. String uname = request.getParameter("txtId");
  297. String upass = request.getParameter("txtPass");
  298. if(uname.equals("sid") && upass.equals("12345")){
  299. RequestDispatcher rd = request.getRequestDispatcher("WelcomeServlet");
  300. rd.forward(request, response); }
  301. else{
  302. out.println("<body bgcolor=red >");
  303. out.println("<h1> Login Fail !!! </h1>");
  304. RequestDispatcher rd = request.getRequestDispatcher("index.html");
  305. rd.include(request, response);}
  306. out.println("</body>");
  307. out.println("</html>");}}
  308.  
  309. WelcomeServlet.java
  310. import java.io.IOException;
  311. import java.io.PrintWriter;
  312. import javax.servlet.ServletException;
  313. import javax.servlet.http.HttpServlet;
  314. import javax.servlet.http.HttpServletRequest;
  315. import javax.servlet.http.HttpServletResponse;
  316. import javax.servlet.RequestDispatcher;
  317. public class WelcomeServlet extends HttpServlet {
  318. public void doGet(HttpServletRequest request, HttpServletResponse response)
  319. throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
  320. out.println("<html><head>");
  321. out.println("<title>Servlet LoginServlet</title></head>"); String uname = request.getParameter("txtId");
  322. String upass = request.getParameter("txtPass");
  323. if(uname.equals("sid") && upass.equals("servlet")){
  324. RequestDispatcher rd = request.getRequestDispatcher("LoginServlet");
  325. rd.forward(request, response);}
  326. else{
  327. out.println("<body bgcolor=blue >");
  328. out.println("<h1> welcome </h1>");
  329. RequestDispatcher rd = request.getRequestDispatcher("index.html");
  330. rd.include(request, response);}
  331. out.println("</body>");
  332. out.println("</html>"); } }
  333.  
  334.  
  335. 13.Create a JSP page to demonstrate the use of Expression language.
  336.  
  337. 14.Develop a web application to allow the user to enter two numbers and select an operation [ +,-,*,/ ] in an HTML file. When the user clicks “Calculate” button, call a servlet to compute the answer and display it to the user.
  338.  
  339. 15.Develop a simple JSP application to pass values from one page to another with validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
  340.  
  341. index.html
  342.  
  343. <html><head><title>User Information Paage</title> </head>
  344.  
  345. <body>
  346. <form action="Validate.jsp">
  347. Enter Your Name<input type="text" name="name" ><br> Enter Your Age<input type="text" name="age" ><br> Select Hobbies
  348. <input type="checkbox" name="hob" value="Singing">Singing
  349. <input type="checkbox" name="hob" value="Reading">Reading Books <input type="checkbox" name="hob" value="Football">Playing Football
  350. <br>
  351. Enter E-mail<input type="text" name="email" ><br> Select Gender
  352. <input type="radio" name="gender" value="male">
  353. Male
  354. <input type="radio" name="gender" value="female">
  355. Female
  356. <input type="radio" name="gender" value="other">
  357. Other<br>
  358. <input type="hidden" name="error" value="">
  359. <input type="submit" value="Submit Form">
  360. </form> </body> </html>
  361. Validate.jsp
  362. <%@page contentType="text/html" pageEncoding="UTF-8"%><html><head><title>JSP Page</title></head><body>
  363.  
  364. <h1>Validation Page</h1>
  365.  
  366. <jsp:useBean id="obj" scope="request"
  367. class="mypack.CheckerBean">
  368. <jsp:setProperty name="obj" property="*"/>
  369. </jsp:useBean>
  370. <%if (obj.validate())
  371. { %>
  372. <% }
  373. else {%>
  374. <jsp:include page="index.html"/>
  375. <%}%>
  376. <%=obj.getError() %>
  377. </body></html>
  378. CheckerBean.java
  379. package mypack;
  380. public class CheckerBean {
  381. private String name, age, hob, email, gender, error;
  382. public CheckerBean(){error="";}
  383. public void setName(String n){name=n;}
  384. public void setAge(String a){age=a;}
  385. public void setHob(String h){hob=h;}
  386. public void setEmail(String e){email=e;}
  387. public void setGender(String g){gender=g;}
  388. public void setError(String e){error=e;}
  389. public String getName(){return name;}
  390. public String getAge(){return age;}
  391. public String getHob(){return hob;}
  392. public String getEmail(){return email;}
  393. public String getGender(){return gender;}
  394. public String getError(){return error;}
  395. public boolean validate(){
  396. boolean res=true;
  397. if(name.trim().equals("")) {error+="<br>Enter First Name";res=false;}
  398. if(age.length() > 2 )
  399. {error+="<br>Age Invalid";res=false;}
  400. return res; } }
  401. 16.Write a JSP to accept eno, ename and salary. Insert these records in emp. (Create the emp table with these three fields.]
Add Comment
Please, Sign In to add comment