Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

PersonalizeWelcome servlet question...

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(...)
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This tiny ad is wafer thin:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic