Before I got to submit my assignment, the teacher told me that one of the requirements for the project was to use a servletcontextlistener. I read all about it but I don't have the practical skills to implement it. Can someone please explain how I can easily implement this method in my program, and it still be useful? Thank you.
The assignment was to build a website (frontend) and make a form (backend). Here is the servlet code, I don't think you need to see the HTML and the JavaScript.
@WebServlet("/ServletProjekt")
public class ServletProjekt extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("förnamn");
String lastName = request.getParameter("efternamn");
String address = request.getParameter("address");
String email = request.getParameter("email");
String howoften = request.getParameter("howoften");
String newsabout = request.getParameter("news");
String[] age = request.getParameterValues("age");
Cookie firstname = new Cookie ("firstname", firstName);
Cookie lastname = new Cookie ("lastname", lastName);
Cookie Address = new Cookie ("Address", address);
Cookie mail = new Cookie ("mail", email);
Cookie HowOften = new Cookie ("HowOften?", howoften);
Cookie news = new Cookie ("news", newsabout);
firstname.setMaxAge(60 * 60 * 24);
lastname.setMaxAge(60 * 60 * 24);
Address.setMaxAge(60 * 60 * 24);
mail.setMaxAge(60 * 60 * 24);
HowOften.setMaxAge(60 * 60 * 24);
news.setMaxAge(60 * 60 * 24);
response.addCookie(firstname);
response.addCookie(lastname);
response.addCookie(Address);
response.addCookie(mail);
response.addCookie(HowOften);
response.addCookie(news );
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.write("<html>");
printWriter.write("<h1> THANK YOU FOR YOUR INFORMATION</h1>");
printWriter.write("<body>");
printWriter.write("<p>Your name:</p>" + firstName);
printWriter.write("<br>");
printWriter.write("<p>Your last name:</p>" +lastName + "<br>");
printWriter.write("<p>Your address:</p>" +address + "<br>");
printWriter.write("<p>Your choice of often:</p>" +howoften + "<br>");
printWriter.write("<p>Your choice of news:</p>" +newsabout + "<br>");
printWriter.write("<p>Your email:</p>" +email + "<br>");
for (String ages : age) {
printWriter.write("<p>Your age:</p>" +ages + "<br>");
}
printWriter.write("<a href =\"GetAllCookies\">View All Cookies</a>");
printWriter.write("</body>");
printWriter.write("</html>");
}
}
implement following interface:
javax.servlet.ServletContextListener
and include the following in your web.xml(replace the class name with the correct one):
<listener>
<listener-class>com.something.MyContextListener</listener-class>
</listener>
firstly you need to create like this
public class yourServletContextListener
implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
//Run this before web application is started
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
}
}
and write this in your web.xml
<listener>
<listener-class>
fully qualified name for yourServletContextListener
</listener-class>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.