Sep 11, 2014

Google App Engine using java servlet



This post is the Google App Engine using java servlet.

Java servlet is server side class.

If you using java servlet that possible to use html language in java.

First, make new project for google app engine.



Non check sample code!!

I make 3 java files.

Second, coding.


This is main.java

  1. @SuppressWarnings("serial")
  2. public class main extends HttpServlet{
  3.     @Override
  4.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  5.             throws ServletException, IOException {
  6.         // TODO Auto-generated method stub
  7.         //super.doGet(req, resp);
  8.         resp.setContentType("text/html; charset=UTF-8");
  9.         PrintWriter show=resp.getWriter();
  10.        
  11.         show.println("메인 페이지 입니다. </BR>");
  12.         show.println("당신의 IP는 "+req.getRemoteAddr()+" 입니다.  </BR>");
  13.        
  14.        
  15.         show.println("1번으로 가실려면 <a href=/num1>이곳</a>을 눌러주세요.</BR>");
  16.         show.println("2번으로 가실려면 <a href=/num2>이곳</a>을 눌러주세요.</BR>");
  17.     }
  18.    
  19. }
Line 2 : java servlet class

Line 9 : for Korean(UTF-8)

Line 10~17 : print to web browser. possible to using html.


Third, Mapping.




This is web.xml


  1.     <servlet>
  2.         <servlet-name>main</servlet-name>
  3.         <servlet-class>com.answerofgod.test.main</servlet-class>
  4.        
  5.     </servlet>
  6.     <servlet-mapping>
  7.         <servlet-name>main</servlet-name>
  8.         <url-pattern>/</url-pattern>
  9.     </servlet-mapping>

Line 1~5 : servlet define.

Line 6~9 : servlet mapping.


Forth, Deploy to App Engine.




Connect to webpage.



MAIN


NUM1

NUM2

No comments:

Post a Comment