Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index.html file
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="ISO-8859-1">
- <title>GetPost</title>
- </head>
- <body>
- <form name="frm1" method="get" action="getpostservlet">
- username : <input type="text" name="uname">
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- Servlet code to print user name back to the user
- package satish;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class getpostservlet
- */
- public class getpostservlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public getpostservlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.print("welcome");
- String username = request.getParameter("uname");
- out.print(username);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- doGet(request, response);
- }
- }
- Servlet Code to print a table to the user
- package satish;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class getpostservlet
- */
- public class getpostservlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public getpostservlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- //writing a table to the user from servlet
- out.print("<table border=1>"
- + "<tr><td>Name</td><td>Reg</td></tr>"
- + "<tr><td>Satish</td><td>123</td></tr>"
- + "<tr><td>Ram</td><td>124</td></tr>"
- + "</table>"
- );
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- doGet(request, response);
- }
- }
- Connecting to a database from within a servlet
- HTML File
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="ISO-8859-1">
- <title>GetPost</title>
- </head>
- <body>
- <form name="frm1" method="get" action="getpostservlet">
- username : <input type="text" name="uname">
- password : <input type="text" name="upass">
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- Servlet Code
- package satish;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class getpostservlet
- */
- public class getpostservlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public getpostservlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String uname = request.getParameter("uname");
- String password = request.getParameter("upass");
- try
- {
- Connection con;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vit","root","");
- Statement stmt = con.createStatement();
- int i = stmt.executeUpdate("insert into user values ('"+uname+"','"+password+"')");
- out.println("Record Inserted Successfully");
- stmt.close();
- con.close();
- }
- catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e)
- {
- out.println(e.getMessage());
- }
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- doGet(request, response);
- }
- }
- Retrieving all Records from a database using servlet
- package satish;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class getpostservlet
- */
- public class getpostservlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public getpostservlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- String uname = request.getParameter("uname");
- String password = request.getParameter("upass");
- try
- {
- Connection con;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vit","root","");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from user");
- if(rs.next()==false)
- {
- out.println("No Records in the table");
- }
- else
- {
- do
- {
- out.print(rs.getString(1));
- out.print(rs.getString(2));
- }while(rs.next());
- }
- stmt.close();
- rs.close();
- con.close();
- }
- catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e)
- {
- out.println(e.getMessage());
- }
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- doGet(request, response);
- }
- }
Add Comment
Please, Sign In to add comment