This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Servlets and the fly likes PersonalizeWelcome servlet question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "PersonalizeWelcome servlet question..." Watch "PersonalizeWelcome servlet question..." New topic

PersonalizeWelcome servlet question...

Shuaib Gill
Ranch Hand

Joined: May 29, 2001
Posts: 62
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)
<title> Servlet example </title>
<meta name = description content = "interactive servlet example">
<meta name = author content = "a valued employee">
<body><form method = get action = "../servlet/PersonalizeWelcome">
Username please
<input type = text name = "name"><p>
<input type = submit>
...and the java code is:
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
PrintWriter out = res.getWriter();
// some intro HTML...
out.println("<title> Servlet example </title>");
out.println("<body><form method = get>");
out.println("Username please");
out.println("<input type = text name = 'name'><p>");
out.println("<input type = submit>");
String remoteUser = req.getRemoteUser(); // why does it not send back req from HTML?
if(remoteUser == null)
out.println("Welcome, " + remoteUser + "!");
Date lastAccess = (Date) accesses.get(remoteUser);
if(lastAccess == null)
out.println("This is your first visit");
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(...)

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

getRemoteUser doesn't do what you think it does.

you type a name into a text field named 'name', and submit it from a form as part of a post or get, there is no other way to get this data, other than request.getParameter("name");

getRemoteUser is involved with login and security. If your user is not authenticated (and probably not, since they will be the anonymous guest user if you haven't got a whole security and authentication layer around your servlet)... then getRemoteUser will always return null.
I agree. Here's the link:
subject: PersonalizeWelcome servlet question...
It's not a secret anymore!