Show List
Servlet Coding Questions
- Write a Servlet that displays "Hello, World!" on the browser.
javaCopy code
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><head><title>Hello, World!</title></head>");
out.println("<body><h1>Hello, World!</h1></body></html>");
}
}
- Write a Servlet that accepts a name parameter and displays "Hello, {name}!" on the browser.
javaCopy code
public class HelloNameServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
if (name == null || name.isEmpty()) {
response.sendRedirect("/error");
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Hello, " + name + "!</title></head>");
out.println("<body><h1>Hello, " + name + "!</h1></body></html>");
}
}
}
- Write a Servlet that counts the number of times it has been accessed and displays the count on the browser.
javaCopy code
public class CountServlet extends HttpServlet {
private int count = 0;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
count++;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Count</title></head>");
out.println("<body><h1>Count: " + count + "</h1></body></html>");
}
}
- Write a Servlet that reads a text file and displays its contents on the browser.
javaCopy code
public class FileServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("filename");
if (filename == null || filename.isEmpty()) {
response.sendRedirect("/error");
} else {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>" + filename + "</title></head>");
out.println("<body><h1>" + filename + "</h1><pre>");
while ((line = reader.readLine()) != null) {
out.println(line);
}
out.println("</pre></body></html>");
reader.close();
}
}
}
- Write a Servlet that accepts a username and password parameter and displays "Welcome, {username}!" on the browser if the username and password are correct, otherwise displays an error message.
javaCopy code
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
response.sendRedirect("/error");
} else if (username.equals("admin") && password.equals("admin123")) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Welcome, " + username + "!</title></head>");
out.println("<body><h1>Welcome, " + username + "!</h1></body></html>");
} else {
response.sendRedirect("/error");
}
}
}
- Write a Servlet that displays a list of items and allows the user to add new items to the list.
csharpCopy code
public class ItemListServlet extends HttpServlet {
private List<String> items = new ArrayList<>();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Item List</title></head>");
out.println("<body><h1>Item List</h1>");
out.println("<ul>");
for (String item : items) {
out.println("<li>" + item + "</li>");
}
out.println("</ul>");
out.println("<form method=\"post\">");
out.println("Item: <input type=\"text\" name=\"item\">");
out.println("<input type=\"submit\" value=\"Add\">");
out.println("</form>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String item = request.getParameter("item");
if (item != null && !item.isEmpty()) {
items.add(item);
}
response.sendRedirect("/itemlist");
}
}
- Write a Servlet that allows the user to upload a file and saves it to the server.
javaCopy code
public class FileUploadServlet extends HttpServlet {
private static final String UPLOAD_DIR = "uploads";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String applicationPath = request.getServletContext().getRealPath("");
String uploadPath = applicationPath + File.separator + UPLOAD_DIR;
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
Part part = request.getPart("file");
String fileName = part.getSubmittedFileName();
part.write(uploadPath + File.separator + fileName);
response.sendRedirect("/fileupload");
}
}
Leave a Comment