MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MainClass extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, java.io.IOException {

        res.setContentType("text/html");
        java.io.PrintWriter out = res.getWriter();
        out.println("<HTML>");
        out.println("<HEAD><TITLE>User Example</TITLE></HEAD>");
        out.println("<BODY>");

        String username = req.getRemoteUser();
        if (username == null) {
            out.println("Hello. You are not logged in.");
        } else if ("Bob".equals(username)) {
            out.println("Hello, Bob. Nice to see you again.");
        } else {
            out.println("Hello, " + username + ".");
        }
        out.println("</BODY>");
        out.println("</HTML>");
        out.close();
    }
}