執行結果:程式碼:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
黃彥霖 發表在 痞客邦 留言(0) 人氣(684)
Postgresql JSR 檔要放在 ROOT\WEB-INF\lib 底下 (這裡用 Tomcat)
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.* " %>
<%@ page import="java.io.*" %>
Hello :
<%
try {
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/mydb";
String username = "postgres";
String password = "a";
String myDataField = null;
String myQuery = "SELECT * FROM users";
Connection myConnection = null;
PreparedStatement myPreparedStatement = null;
ResultSet myResultSet = null;
Class.forName(driver).newInstance();
myConnection = DriverManager.getConnection(url,username,password);
myPreparedStatement = myConnection.prepareStatement(myQuery);
myResultSet = myPreparedStatement.executeQuery();
while(myResultSet.next())
myDataField = myResultSet.getString("name");
out.print(myDataField);
}
黃彥霖 發表在 痞客邦 留言(0) 人氣(747)
1. 先在 eclipse 安裝 ADT: https://dl-ssl.google.com/android/eclipse/
2. 之後再到這個網頁查看最新版本的 Google Plugin for Eclipse:
https://developers.google.com/eclipse/docs/download?hl=zh-TW
取得像 http://dl.google.com/eclipse/plugin/4.3 的網址並在 Eclipse 安裝
黃彥霖 發表在 痞客邦 留言(0) 人氣(667)
在 doGet() 方法裡面加入以下程式碼,即可將 Servlet 設置為 UTF-8 編碼,理所當然也可以顯示中文:
response.setContentType("text/html;charset=UTF-8");
黃彥霖 發表在 痞客邦 留言(0) 人氣(2,761)
response.sendRedirect("index.jsp");
request.getRequestDispatcher("index.jsp").forward(request, response);
黃彥霖 發表在 痞客邦 留言(0) 人氣(332)
要放在 WEB-INF / classes / 底下
例如 WEB-INF / classes / test / MyClass.java
XML 則寫 class.test.MyClass
黃彥霖 發表在 痞客邦 留言(0) 人氣(190)
學寫 JSP 之前,你要先學會 HTML 語法,因為 JSP 是寫在 HTML 語法裡面的,如下
<HTML>
<%
// JSP 寫在這
黃彥霖 發表在 痞客邦 留言(0) 人氣(895)