Table of Contents
hide
Example: A Servelets program to print a message.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello, World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
---------- OR -----------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello World!</h1>");
out.println("</body></html>");
}
}
Example: A Servelets program to print user-accepted data from a Form.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FormServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Form Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Form Data</h1>");
out.println("<p>User Name: " + request.getParameter("uname") + "</p>");
out.println("<p>User Email: " + request.getParameter("uemail") + "</p>");
out.println("</body>");
out.println("</html>");
}
}
NB: Here, 'uname' and 'uemail' are the name of html Form user name and user email box.
Example: A Servelet program to handle form submissions and displays the submitted data on the web page.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FormServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<form method='post' action=''>");
out.println("Name: <input type='text' name='uname'><br>");
out.println("Email: <input type='text' name='uemail'><br>");
out.println("<input type='submit' value='Submit'>");
out.println("</form>");
String name = request.getParameter("uname");
String email = request.getParameter("uemail");
if (uname != null && uemail != null) {
out.println("<p>User Name: " + uname + "</p>");
out.println("<p>User Email: " + uemail + "</p>");
}
out.println("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Example: A Servelets program to print user-accepted data from a Form/web page.
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DatabaseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Database Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Retrieve MySql Database Data</h1>");
try {
// Load the JDBC driver for MySql database
Class.forName("com.mysql.jdbc.Driver");
// Connectivity code to the MySql database
String url = "jdbc:mysql://localhost/databasename";
String username = "root";
String password = "password";
Connection conn = DriverManager.getConnection(url, username, password);
// Create the SQL statement
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM tablename";
// Execute the SQL statement
ResultSet rs = stmt.executeQuery(sql);
// Display the results
while(rs.next()) {
out.println("<p>User Name: " + rs.getString("uname") + "</p>");
out.println("<p>User Email: " + rs.getString("uemail") + "</p>");
}
// Clean up
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
out.println("<p>Error: " + e.getMessage() + "</p>");
}
out.println("</body>");
out.println("</html>");
}
}
Example: A Servelets program to store user data in a session and displays it on the web page.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String vname = request.getParameter("uname");
if (vname != null) {
session.setAttribute("name", sname);
}
out.println("<html><body>");
out.println("<form method='post' action=''>");
out.println("Name: <input type='text' name='uname'><br>");
out.println("<input type='submit' value='Submit'>");
out.println("</form>");
String sessionName = (String)session.getAttribute("sname");
if (sessionName != null) {
out.println("<p>Name stored in session: " + sessionName + "</p>");
}
out.println("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
0 Comments