MyServlet.java Source code

Java tutorial

Introduction

Here is the source code for MyServlet.java

Source

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String pin = req.getParameter("pin");

        if (!allowUser(account, password, pin)) {
            out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
            out.println("<BODY>Your login and password are invalid.<BR>");
            out.println("You may want to <A HREF=\"/login.html\">try again</A>");
            out.println("</BODY></HTML>");
        } else {
            out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
            out.println("<BODY>Your login and password are valid.<BR>");
            out.println("</BODY></HTML>");
            HttpSession session = req.getSession();
            session.setAttribute("logon.isDone", account); // just a marker object

            try {
                String target = (String) session.getAttribute("login.target");
                if (target != null) {
                    res.sendRedirect(target);
                    return;
                }
            } catch (Exception ignored) {
            }

            //res.sendRedirect("/");
        }
    }

    protected boolean allowUser(String account, String password, String pin) {
        return true;
    }
}