執行結果:
程式碼:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
執行結果:
程式碼:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
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";
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 安裝
3. 修改 Eclipse 安裝檔底下的 eclipse.ini 檔:
A:插入 ( 依造自己 JDK 位置修改 )
在 doGet() 方法裡面加入以下程式碼,即可將 Servlet 設置為 UTF-8 編碼,理所當然也可以顯示中文:
response.setContentType("text/html;charset=UTF-8");
// 如果是接收參數的亂碼,請參考以下:
String name = new String(request.getParameter("name").getBytes("iso-8859-1"), "UTF-8");
response.sendRedirect("index.jsp");
request.getRequestDispatcher("index.jsp").forward(request, response);
要放在 WEB-INF / classes / 底下
例如 WEB-INF / classes / test / MyClass.java
XML 則寫 class.test.MyClass
在NetBeans開發時,一定要加上 classes 資料夾,否則無法找到下層資料夾,網頁會發生錯誤
學寫 JSP 之前,你要先學會 HTML 語法,因為 JSP 是寫在 HTML 語法裡面的,如下
<HTML>
<%
// JSP 寫在這
%>
</HTML>
然後儲存附檔名為:[ .jsp ] 而不是 .html,因為只有 .jsp 檔才會被編譯,如果你是寫 .html 網頁只能顯示你 JSP語法而已,結果會很蠢...