แสดงบทความที่มีป้ายกำกับ Java แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Java แสดงบทความทั้งหมด

18 ส.ค. 2556

Example Servlet

Java Servlet Example

http://localhost:8080/yourwebsiteHelloForm?first_name

การสร้างโปรเจคใน Tomcat จะใช้รูปแบบโครงสร้างตามนี้

path "To tomcat folder -> apache-tomcat-x.x/webapps/yourwebsite"

 

ภายใน WEB-INF 

1. classes ที่อยู่ของ class file java

2. lib ที่ใช้งาน

3. web.xml ไฟล์ config ของ servlet

 

ด้านล่างนี้เป็น class com.example.HelloServlet สำหรับรับค่า Input จาก Browser 

โดยใช้ getParameter() ในการรับค่า

package com.example;

// Import required java libraries
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Extend HttpServlet class
public class HelloServlet extends HttpServlet {

 // Method to handle GET method request.
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  // Set response content type
  response.setContentType("text/html");

  PrintWriter out = response.getWriter();
  String title = "Using GET Method to Read Form Data";
  String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
    + "transitional//en\">\n";
  out.println(docType + "<html>\n" + "<head><title>" + title
    + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"
    + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n"
    + "  <li><b>First Name</b>: "
    + request.getParameter("first_name") + "\n"
    + "  <li><b>Last Name</b>: "
    + request.getParameter("last_name") + "\n" + "</ul>\n"
    + "</body></html>");
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
}


ไฟล์ web.xml 

ใช้สำหรับ การอ้าง class ที่เราจะทำการ Mapping กับ Url


  <servlet>
       <servlet-name>HelloServlet</servlet-name>
       <servlet-class>com.example.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
       <servlet-name>HelloServlet</servlet-name>
       <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping> 
 
 
 
 
 

hello.jsp หน้าเว็บ Html ใช้ Get Method ในการ เรียก

 

<html> <body>  
<form action="HelloServlet" method="GET">
      First Name: <input type="text" name="first_name"> <br />
      Last Name: <input type="text" name="last_name" />
      <input type="submit" value="Submit" />  
</form>  
</body></html>

26 มิ.ย. 2555

Install tomcat on window

Install tomcat server for use war file Generate by Java

1. Download Tomcat 6  http://tomcat.apache.org/download-60.cgi
 and extract zip file to drive C:\Program Files

2. Download Java JDK (Java SE 6 Update 33) http://www.oracle.com/technetwork/java/javase/downloads/index.html
 and install set JAVA_HOME by path of JDK "C:/Program Files/java/jdk/" not JRE

3. After install Java use "cmd" go to "~/tomcat/bin" and "service.bat install" in "cmd" 
Go to Control Panel Adminstrator Tools -> Service and Start "Apache Tomcat Server" open http://localhost:8080 or http://127.0.0.1:8080 Work!! 
but don't  open  try to close Window Firewall

4. Copy war file "yourservice.war" to C:\Program Files\apache-tomcat-6\webapps and url:http://localhost:8080/yourservice

SET Encoding for Tomcat Window

Try setting the (Windows) environment 

variable JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF-8and 

start the Tomcat Server.

18 มิ.ย. 2555

Play Framework ทำเว็บด้วย Java กัน

  1. Download file play framework จาก http://www.playframework.org/ แตก zip ไฟล์ที่ได้ไว้ใน  Directory นึง

  2. Set Envoirment path โดยใส่ path ของ Directory Play framework ที่เรา แตก zip ไว้ restart เครื่อง
  3. สร้าง Application โดยทำการเปิดหน้าต่าง command line หรือ cmd นั่นเอง พิมพ์
    "play new ชื่อProject " เมื่อพิมพ์เสร็จแล้วก็จะได้ Folder ที่ข้างในจะมี ส่วนของการทำงานต่างๆโดยจะแบ่งเป็น Folder ดังนี้
    1.   "app/" contains the application’s core, split between models, controllers and views directories. This is the directory where .java source files live.
    2.   "conf/" contains all the application’s configuration files, especially the main appliation.conffile, the routes definition files and the messages files used for internationalization.
    3.   "project/" contains the build scripts. The build system is based on sbt. But a new play application comes with a default build script that will just works fine for our application.
    4.   "public/" contains all the publicly available resources, which includes JavaScript, stylesheets and images directories.
    5.   "test/" contains all the application tests. Tests can be written as JUnit tests. 

  4. เข้าไปใน Folder Project เราพิมพ์ "play" จะขึ้นชื่อ Project มาต่อไปพิมพ์ "run" รอสักพัก ก็จะขึ้นแบบนี้


    ภาพจาก http://www.playframework.org/documentation/2.0.1/JavaTodoList

    ทำการเข้าเมื่อโปรแกรม run เสร็จแล้วที่ http://localhost:9000/




    วันนี้เท่านี้ก่อนนะครับวันหลังจะมาต่อเรื่องการใช้ play ร่วมกับ IDE Eclipse