File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "PersonalizeWelcome servlet question..." Watch "PersonalizeWelcome servlet question..." New topic
Author

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)
<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(...)


programmer77
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: http://aspose.com/file-tools
 
subject: PersonalizeWelcome servlet question...
 
Similar Threads
How do I use getRemoteUser()
Internal service error..
How to find the userid of my clients?
Progress bar on IE6
Waiting page while loading