Guest User

iwp

a guest
Apr 3rd, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. Index.html file
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="ISO-8859-1">
  6. <title>GetPost</title>
  7. </head>
  8. <body>
  9. <form name="frm1" method="get" action="getpostservlet">
  10. username : <input type="text" name="uname">
  11. <input type="submit" value="Submit">
  12.  
  13. </form>
  14. </body>
  15. </html>
  16.  
  17. Servlet code to print user name back to the user
  18. package satish;
  19.  
  20. import java.io.IOException;
  21. import java.io.PrintWriter;
  22.  
  23. import javax.servlet.ServletException;
  24. import javax.servlet.annotation.WebServlet;
  25. import javax.servlet.http.HttpServlet;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28.  
  29. /**
  30. * Servlet implementation class getpostservlet
  31. */
  32.  
  33. public class getpostservlet extends HttpServlet {
  34. private static final long serialVersionUID = 1L;
  35.  
  36. /**
  37. * @see HttpServlet#HttpServlet()
  38. */
  39. public getpostservlet() {
  40. super();
  41. // TODO Auto-generated constructor stub
  42. }
  43.  
  44. /**
  45. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  46. */
  47. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  48. // TODO Auto-generated method stub
  49. response.setContentType("text/html");
  50. PrintWriter out = response.getWriter();
  51. out.print("welcome");
  52. String username = request.getParameter("uname");
  53. out.print(username);
  54.  
  55. }
  56.  
  57. /**
  58. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  59. */
  60. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  61. // TODO Auto-generated method stub
  62. doGet(request, response);
  63. }
  64.  
  65. }
  66.  
  67.  
  68. Servlet Code to print a table to the user
  69.  
  70. package satish;
  71.  
  72. import java.io.IOException;
  73. import java.io.PrintWriter;
  74.  
  75. import javax.servlet.ServletException;
  76. import javax.servlet.annotation.WebServlet;
  77. import javax.servlet.http.HttpServlet;
  78. import javax.servlet.http.HttpServletRequest;
  79. import javax.servlet.http.HttpServletResponse;
  80.  
  81. /**
  82. * Servlet implementation class getpostservlet
  83. */
  84.  
  85. public class getpostservlet extends HttpServlet {
  86. private static final long serialVersionUID = 1L;
  87.  
  88. /**
  89. * @see HttpServlet#HttpServlet()
  90. */
  91. public getpostservlet() {
  92. super();
  93. // TODO Auto-generated constructor stub
  94. }
  95.  
  96. /**
  97. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  98. */
  99. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  100. // TODO Auto-generated method stub
  101. response.setContentType("text/html");
  102. PrintWriter out = response.getWriter();
  103. //writing a table to the user from servlet
  104. out.print("<table border=1>"
  105. + "<tr><td>Name</td><td>Reg</td></tr>"
  106. + "<tr><td>Satish</td><td>123</td></tr>"
  107. + "<tr><td>Ram</td><td>124</td></tr>"
  108. + "</table>"
  109. );
  110.  
  111. }
  112.  
  113. /**
  114. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  115. */
  116. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  117. // TODO Auto-generated method stub
  118. doGet(request, response);
  119. }
  120.  
  121. }
  122.  
  123. Connecting to a database from within a servlet
  124.  
  125. HTML File
  126. <!DOCTYPE html>
  127. <html>
  128. <head>
  129. <meta charset="ISO-8859-1">
  130. <title>GetPost</title>
  131. </head>
  132. <body>
  133. <form name="frm1" method="get" action="getpostservlet">
  134. username : <input type="text" name="uname">
  135. password : <input type="text" name="upass">
  136. <input type="submit" value="Submit">
  137.  
  138. </form>
  139. </body>
  140. </html>
  141.  
  142.  
  143.  
  144.  
  145. Servlet Code
  146.  
  147. package satish;
  148.  
  149. import java.io.IOException;
  150. import java.io.PrintWriter;
  151. import java.sql.Connection;
  152. import java.sql.DriverManager;
  153. import java.sql.SQLException;
  154. import java.sql.Statement;
  155.  
  156. import javax.servlet.ServletException;
  157. import javax.servlet.annotation.WebServlet;
  158. import javax.servlet.http.HttpServlet;
  159. import javax.servlet.http.HttpServletRequest;
  160. import javax.servlet.http.HttpServletResponse;
  161.  
  162. /**
  163. * Servlet implementation class getpostservlet
  164. */
  165.  
  166. public class getpostservlet extends HttpServlet {
  167. private static final long serialVersionUID = 1L;
  168.  
  169. /**
  170. * @see HttpServlet#HttpServlet()
  171. */
  172. public getpostservlet() {
  173. super();
  174. // TODO Auto-generated constructor stub
  175. }
  176.  
  177. /**
  178. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  179. */
  180. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  181. // TODO Auto-generated method stub
  182. response.setContentType("text/html");
  183. PrintWriter out = response.getWriter();
  184. String uname = request.getParameter("uname");
  185. String password = request.getParameter("upass");
  186. try
  187. {
  188. Connection con;
  189. Class.forName("com.mysql.jdbc.Driver").newInstance();
  190. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vit","root","");
  191. Statement stmt = con.createStatement();
  192. int i = stmt.executeUpdate("insert into user values ('"+uname+"','"+password+"')");
  193. out.println("Record Inserted Successfully");
  194. stmt.close();
  195. con.close();
  196. }
  197. catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e)
  198. {
  199. out.println(e.getMessage());
  200. }
  201.  
  202. }
  203.  
  204. /**
  205. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  206. */
  207. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  208. // TODO Auto-generated method stub
  209. doGet(request, response);
  210. }
  211.  
  212. }
  213.  
  214. Retrieving all Records from a database using servlet
  215. package satish;
  216.  
  217. import java.io.IOException;
  218. import java.io.PrintWriter;
  219. import java.sql.Connection;
  220. import java.sql.DriverManager;
  221. import java.sql.ResultSet;
  222. import java.sql.SQLException;
  223. import java.sql.Statement;
  224.  
  225. import javax.servlet.ServletException;
  226. import javax.servlet.annotation.WebServlet;
  227. import javax.servlet.http.HttpServlet;
  228. import javax.servlet.http.HttpServletRequest;
  229. import javax.servlet.http.HttpServletResponse;
  230.  
  231. /**
  232. * Servlet implementation class getpostservlet
  233. */
  234.  
  235. public class getpostservlet extends HttpServlet {
  236. private static final long serialVersionUID = 1L;
  237.  
  238. /**
  239. * @see HttpServlet#HttpServlet()
  240. */
  241. public getpostservlet() {
  242. super();
  243. // TODO Auto-generated constructor stub
  244. }
  245.  
  246. /**
  247. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  248. */
  249. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  250. // TODO Auto-generated method stub
  251. response.setContentType("text/html");
  252. PrintWriter out = response.getWriter();
  253. String uname = request.getParameter("uname");
  254. String password = request.getParameter("upass");
  255. try
  256. {
  257. Connection con;
  258. Class.forName("com.mysql.jdbc.Driver").newInstance();
  259. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vit","root","");
  260. Statement stmt = con.createStatement();
  261. ResultSet rs = stmt.executeQuery("select * from user");
  262. if(rs.next()==false)
  263. {
  264. out.println("No Records in the table");
  265. }
  266. else
  267. {
  268. do
  269. {
  270. out.print(rs.getString(1));
  271. out.print(rs.getString(2));
  272. }while(rs.next());
  273.  
  274. }
  275.  
  276. stmt.close();
  277. rs.close();
  278. con.close();
  279.  
  280. }
  281. catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e)
  282. {
  283. out.println(e.getMessage());
  284. }
  285. }
  286.  
  287. /**
  288. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  289. */
  290. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  291. // TODO Auto-generated method stub
  292. doGet(request, response);
  293. }
  294.  
  295. }
Add Comment
Please, Sign In to add comment