using xml
Mon Nov 04 2024 01:10:30 GMT+0000 (Coordinated Universal Time)
Saved by @signup1
package com.example.test; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * Servlet implementation class UserServlet */ @WebServlet("/UserServlet") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UserServlet() { super(); System.out.println("Servlet is Instantiated, called once in its lifetime"); } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { System.out.println("called once for initialization of servlet"); } /** * @see Servlet#destroy() */ public void destroy() { System.out.println("Servlet is terminated"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Retrieve form data String name = request.getParameter("name"); String email = request.getParameter("email"); String course = request.getParameter("course"); String branch = request.getParameter("branch"); String hobbies = request.getParameter("hobbies"); // Generate HTML response out.println("<html><body>"); out.println("<h1>Welcome, " + name + "!</h1>"); out.println("<p>Email: " + email + "</p>"); out.println("<p>Course: " + course + "</p>"); out.println("<p>Branch: " + branch + "</p>"); out.println("<p>Hobbies: " + hobbies + "</p>"); out.println("</body></html>"); } } //UserServlet.html <html> <body> <form action="UserServlet" method="POST"> Name: <input type="text" name="name"><br> Email: <input type="text" name="email"><br> Course: <input type="text" name="course"><br> Branch: <input type="text" name="branch"><br> Hobbies: <input type="text" name="hobbies"><br> <input type="submit" value="Submit"> </form> </body> </html> // program on request parameters package com.example.test; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * Servlet implementation class VotingServlet */ @WebServlet("/Voting") public class VotingServlet extends HttpServlet { private static final long serialVersionUID = 1L; public VotingServlet() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { System.out.println("Init method is called"); } /** * @see Servlet#destroy() */ public void destroy() { System.out.println("Im done with work,Destroy method is called"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); String name=request.getParameter("Name"); String age=request.getParameter("Age"); int a=Integer.parseInt(age); response.setContentType("text/html"); PrintWriter pw=response.getWriter(); if(a<18) pw.println("<h1> You "+name +" are not Eligble for casting vote!! Wait upto 18 years</h1>"); else pw.println("<h1> Congrats!!! "+name+" You are Eligble for casting vote!! </h1>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } //VotingInterface.html <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <style> div{ background-color:lightgreen; color:Red; padding:20px; margin:20px; border:2px yellow; } </style> </head> <body> <div> <h1>Checking Voting Credentials </h1> <form action="Voting" method="get"> <label>Username :</label><input type="text" name="Name" > <br> <label>Age: </label><input type="text" name="Age" > <br><input type="submit" name="submit"> </form> </div> </body> </html> // 3 . init parameters <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Login Page</title> </head> <body> <h2>Login Form</h2> <form method="POST" action="logininit"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Login"> </form> </body> </html> package com.example.test; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServletInit @WebServlet("/Logininit")*/ public class LoginServletInit extends HttpServlet { private static final long serialVersionUID = 1L; private String validUsername; private String validPassword; private String email; /** * Default constructor. */ public LoginServletInit() { // TODO Auto-generated constructor stub } // The init method reads the init parameters defined in web.xml @Override public void init(ServletConfig config) throws ServletException { super.init(config); validUsername = config.getInitParameter("username"); validPassword = config.getInitParameter("password"); email=config.getInitParameter("email"); } // Handle POST request for login validation @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Retrieve user input from the login form String username = request.getParameter("username"); String password = request.getParameter("password"); // Set the response type response.setContentType("text/html"); PrintWriter out = response.getWriter(); System.out.println("email = "+email ); // Check credentials if (validUsername.equals(username) && validPassword.equals(password)) { out.println("<html><body>"); out.println("<h1>Login successful! Welcome, " + username + "Your email is "+email+" </h1>"); out.println("</body></html>"); } else { out.println("<html><body>"); out.println("<h1>Invalid credentials. Please try again.</h1>"); out.println("<a href=\"logininitparameter.html\">Go back to Login</a>"); out.println("</body></html>"); } out.close(); } } //web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <servlet> <servlet-name>LoginServletInit</servlet-name> <servlet-class>com.example.test.LoginServletInit</servlet-class> <!-- Init parameters for login credentials --> <init-param> <param-name>username</param-name> <param-value>ramesh</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>fattah</param-value> </init-param> <init-param> <param-name>email</param-name> <param-value>ramesh@gmail.com</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>LoginServletInit</servlet-name> <url-pattern>/logininit</url-pattern> </servlet-mapping> </web-app> TIRUNAGARI SHREYA cse2022e
Comments