Hi Chau,
I used the code in the tutorial with a slightly change. Data is posted by
POST method in the form so SignGuestbookServlet should use doPost to handle
it.
Anothe thing I am confused is that I can't see log message in Eclipse
console if I added custom message like log.info or system.out.printlin
alough I givie guestbook.level = INFO in logging.properties.
Code below is SignGuestbookServlet.java:
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
public class SignGuestbookServlet {
private static final Logger log =
Logger.getLogger(SignGuestbookServlet.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String content = req.getParameter("content");
if (content == null) {
content = "(No greeting)";
}
if (user != null) {
log.info("Greeting posted by user " + user.getNickname() + ": "
+ content);
} else {
log.info("Greeting posted anonymously: " + content);
}
resp.sendRedirect("/guestbook.jsp");
}
}
Code below is guestbook.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<html>
<body>
<%
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
%>
<p>Hi, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign
out</a>.)</p>
<%
} else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign
in</a>
to include your name with greetings you post.</p>
<%
}
%>
Comment Board:
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post Greeting" /></div>
</form>
</body>
</html>
code below is GuestbookServlet.java:
package guestbook;
import java.io.IOException;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
@SuppressWarnings("serial")
public class GuestbookServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
//log.info("user name is " + user.getNickname());
if (user != null) {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, " + user.getNickname());
} else {
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
}
}
}
On Fri, Apr 16, 2010 at 12:16 PM, Chau Huynh <[email protected]> wrote:
> And next you'll need to post your servlet ;-)
>
> Just wonder if you code SignGuestbookServlet servlet yourself - in such a
> case, you might code doGet() while your JSP posts to the servlet? Error
> message from your post: "HTTP method POST is not supported by this URL"
>
>
> On Fri, Apr 16, 2010 at 10:59 PM, bosun <[email protected]> wrote:
>
>> Hi Rajeev,
>>
>> Thank you for helping me troubleshooting this issue.
>>
>> Below is web.xml
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns="http://java.sun.com/xml/ns/javaee"
>> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
>> <servlet>
>> <servlet-name>Guestbook</servlet-name>
>> <servlet-class>guestbook.GuestbookServlet</servlet-class>
>> <servlet-name>sign</servlet-name>
>> <servlet-class>guestbook.SignGuestbookServlet</servlet-class>
>> </servlet>
>> <servlet-mapping>
>> <servlet-name>Guestbook</servlet-name>
>> <url-pattern>/guestbook</url-pattern>
>> <servlet-name>sign</servlet-name>
>> <url-pattern>/sign</url-pattern>
>> </servlet-mapping>
>> <welcome-file-list>
>> <welcome-file>guestbook.jsp</welcome-file>
>> </welcome-file-list>
>> </web-app>
>>
>> below is appengine-web.xml
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
>> <application></application>
>> <version>1</version>
>>
>> <!-- Configure java.util.logging -->
>> <system-properties>
>> <property name="java.util.logging.config.file"
>> value="WEB-INF/logging.properties"/>
>> </system-properties>
>>
>> </appengine-web-app>
>>
>>
>>
>>
>> On Fri, Apr 16, 2010 at 10:29 AM, Rajeev Dayal <[email protected]> wrote:
>>
>>> Can you post the contents of your web.xml file and your appengine-web.xml
>>> file?
>>>
>>> On Thu, Apr 15, 2010 at 10:21 PM, bosun <[email protected]> wrote:
>>>
>>>> I forgot mention in my post about the version of Eclipse I am using is
>>>> Galileo. I downloaded Google plugin for this version.
>>>>
>>>> The link to the Tutorial where I am stuck on is:
>>>> http://code.google.com/intl/en/appengine/docs/java/gettingstarted/usingjsps.html
>>>>
>>>> In Eclipse console, all log messages after server restarted are like
>>>> below:
>>>>
>>>> Apr 16, 2010 2:05:54 AM
>>>> com.google.apphosting.utils.config.AppEngineWebXmlReader
>>>> readAppEngineWebXml
>>>> INFO: Successfully processed
>>>> C:\EclipseGalileo\workspace\Guestbook\war\WEB-INF/appengine-web.xml
>>>> Apr 16, 2010 2:05:54 AM
>>>> com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
>>>> INFO: Successfully processed
>>>> C:\EclipseGalileo\workspace\Guestbook\war\WEB-INF/web.xml
>>>> The server is running at http://localhost:8888/
>>>> Apr 16, 2010 2:07:08 AM
>>>> com.google.appengine.tools.development.LocalResourceFileServlet doGet
>>>> WARNING: No file found for: /favicon.ico
>>>> Apr 16, 2010 2:07:25 AM
>>>> com.google.appengine.tools.development.LocalResourceFileServlet doGet
>>>> WARNING: No file found for: /favicon.ico
>>>>
>>>> Please advise where it goes wrong.
>>>>
>>>> Thanks!
>>>>
>>>>
>>>> On Thu, Apr 15, 2010 at 9:57 PM, bobo <[email protected]> wrote:
>>>>
>>>>> Hi
>>>>>
>>>>> I run Google App Engine (Java) locally with Eclipse. Following the
>>>>> tutorial of Getting Started - Java, I lean it step by step and my
>>>>> local server works well until the section Using JSPs. After adding
>>>>> SignGuestbookServlet.java and greeting form in guestbook.jsp as well
>>>>> as editing web.xml for servlet mapping for sign and /sign, I
>>>>> restarted the server. My browser displayed error message immediately
>>>>> after I tried to post a greeting message:
>>>>>
>>>>> HTTP ERROR 405
>>>>> Problem accessing /sign. Reason: HTTP method POST is not supported
>>>>> by this URL
>>>>>
>>>>> In Eclipse console, the red error message like:
>>>>> Apr 16, 2010 1:34:31 AM
>>>>> com.google.appengine.tools.development.LocalResourceFileServlet doGet
>>>>> WARNING: No file found for: /favicon.ico
>>>>>
>>>>> I googled this error but seems no one having this problem before. I
>>>>> am stuck on this point and anyone who can shed me a light?
>>>>>
>>>>> Thanks!
>>>>>
>>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Google App Engine for Java" group.
>>>> To post to this group, send email to
>>>> [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<google-appengine-java%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/google-appengine-java?hl=en.
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Google App Engine for Java" group.
>>> To post to this group, send email to
>>> [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<google-appengine-java%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/google-appengine-java?hl=en.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Google App Engine for Java" group.
>> To post to this group, send email to
>> [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<google-appengine-java%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-appengine-java?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-appengine-java%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.