Hi fellow programmers,
I have the following
servlet but I am always getting the message "Welcome!" no matter what I type in in my HTML form request. I don't get "Welcome, Joe!" when I type in Joe for example. Why is that? I feel the error may be in my HTML code in the form or in the remoteUser() method. Can someone help me to understand why I do not get personalized messages sent back to me from the server? I have to use the getRemoteUser() method, can't use getParameter("name"). How can I use getRemoteUser() and still get the desired output? Here is the code (.java and .html)
<html>
<head>
<title> Servlet example </title>
<meta name = description content = "interactive servlet example">
<meta name = author content = "a valued employee">
</head>
<body><form method = get action = "../servlet/PersonalizeWelcome">
Username please
<input type = text name = "name"><p>
<input type = submit>
</form>
</body>
</html>
...and the
java code is:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PersonalizeWelcome extends HttpServlet
{
Hashtable accesses = new Hashtable();
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// some intro HTML...
/*out.println("<html>");
out.println("<head>");
out.println("<title> Servlet example </title>");
out.println("</head>");
out.println("<body><form method = get>");
out.println("Username please");
out.println("<input type = text name = 'name'><p>");
out.println("<input type = submit>");
out.println("</form>");
out.println("</body>");
out.println("</html>");*/
String remoteUser = req.getRemoteUser(); // why does it not send back req from HTML?
if(remoteUser == null)
{
out.println("Welcome!");
}
else
{
out.println("Welcome, " + remoteUser + "!");
Date lastAccess = (Date) accesses.get(remoteUser);
if(lastAccess == null)
{
out.println("This is your first visit");
}
else
{
out.println("Your first visit was " + accesses.get(remoteUser));
}
if(remoteUser.equals("PROFESSOR FALKEN"))
{
out.println("Shall we play a game?");
}
accesses.put(remoteUser,new Date());
}
}// end doGet(...)
}// end PersonalizeWelcome(...)