[go: up one dir, main page]

0% found this document useful (0 votes)
18 views2 pages

Servlet Examples

The document provides examples of Java servlets, including a LoginServlet for validating user credentials and maintaining sessions, and a QuizServlet for processing quiz answers submitted via an HTML form. The LoginServlet checks if the username and password match predefined values, while the QuizServlet evaluates the user's answer to a simple math question. Both servlets demonstrate basic servlet functionality and session management in a web application context.

Uploaded by

mrakilesh4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Servlet Examples

The document provides examples of Java servlets, including a LoginServlet for validating user credentials and maintaining sessions, and a QuizServlet for processing quiz answers submitted via an HTML form. The LoginServlet checks if the username and password match predefined values, while the QuizServlet evaluates the user's answer to a simple math question. Both servlets demonstrate basic servlet functionality and session management in a web application context.

Uploaded by

mrakilesh4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Servlet Examples

1. Servlet for Validating Credentials and Maintaining a Session

**LoginServlet.java**
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");

if ("admin".equals(username) && "password".equals(password)) {


HttpSession session = request.getSession();
session.setAttribute("user", username);
response.getWriter().println("Login Successful! Welcome, " + username);
} else {
response.getWriter().println("Invalid credentials. Try again.");
}
}
}
```

2. HTML Quiz Form Submitting to a Servlet

**quiz.html**
```html
<form action="QuizServlet" method="post">
<p>What is 2 + 2?</p>
<input type="radio" name="answer" value="3">3<br>
<input type="radio" name="answer" value="4">4<br>
<input type="submit" value="Submit">
</form>
```

**QuizServlet.java**
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/QuizServlet")
public class QuizServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String answer = request.getParameter("answer");
if ("4".equals(answer)) {
response.getWriter().println("Correct!");
} else {
response.getWriter().println("Wrong answer!");
}
}
}
```

... (Other servlet implementations follow similarly)

You might also like