1. 程式人生 > >Java web開發快速上手

Java web開發快速上手

學習程式設計,必須learning by doing。

學習程式設計,千萬不要被某些莫名其妙的細節搞得失去學習的興趣

這裡給同學們一個最快速的Java web上手例子。初學Java Web開發時,先什麼都不需要懂(《程式設計導論(Java)》除外,討論Web開發時,你還敢問Java程式設計的基礎問題),程式碼跑起來最大!


使用FreeMind的直接按照上圖學習,複製其中附帶的程式碼。

1.開發環境

初學Java Web開發時,yqj2065要求你省略掉一切多餘的東西。JSP、xml檔案用記事本,servlet用BlueJ,伺服器用Tomcat解壓版。

在你學習《程式設計導論(Java)》時,已經安裝了JDK、BlueJ的基礎上,JDK環境變數僅需設定一下classpath;

Tomcat的環境變數
(1)變數名: CATALINA_BASE     變數值: D:\JavaWeb\apache-tomcat-7.0.64(Tomcat解壓到的目錄)
(2)變數名: CATALINA_HOME     變數值: D:\JavaWeb\apache-tomcat-7.0.64
(3)變數名: CATALINA_TMPDIR     變數值:D:\JavaWeb\apache-tomcat-7.0.64\temp
(4)變數名: Path                                  變數值:D:\JavaWeb\apache-tomcat-7.0.64\bin

cdm-startup.bat啟動服務,點選http://localhost:8080/能夠看見預設的頁面為準。

BlueJ:需要將Tomcat自帶的servlet-api.jar複製一份丟到BlueJ的BlueJ\lib\userlib中即可,編譯servlet需要它。

2.HelloWorld

目標:在客戶端瀏覽器輸出字串。3步:

1)建立若干資料夾;編寫基本(以後一直要在其中修改)的2)HelloWorld\index.jsp和3)HelloWorld\WEB-INF\web.xml。

你可以在tomcat\webapps\examples中提供的例子上修改,也可以用腦圖中附帶的例子。如index.jsp

<%@page language="java" pageEncoding="gbk"%>
<html>
<head><title>Apache Tomcat Examples-HelloWorld</title></head>
<body>HelloWorld</body>
</html>
web.xml如下:
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>
啟動服務,點選http://localhost:8080/HelloWorld/(大小寫敏感)。

3.增強HelloWorld

1. 編寫HelloServlet。(預設情況下,Web應用HelloWorld使用的servlet,其類檔案應該放在WEB-INF/classes中。如果類全名為com.myorg.MyServlet,它的路徑必須為WEB-INF/classes/com/myorg/MyServlet.class。)

BlueJ在資料夾HelloWorld\WEB-INF建立專案classes。(一勞永逸)。複製貼上下面的程式碼(馬上會新增package語句),編譯。

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Write a description of class HelloServlet here.
 *
 * @author (yqj2065)
 * @version (0.1)
 */

 
/**
 * Servlet implementation class for Servlet: HelloServlet
 *
 */
 public class HelloServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
        private String target = "/hello.jsp";
    /**
        *
        */
       private static final long serialVersionUID = -3522462295690035558L;
 
       /* (non-Java-doc)
        * @see javax.servlet.http.HttpServlet#HttpServlet()
        */
       public HelloServlet() {
              super();
       }        
     
       /* (non-Java-doc)
        * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              response.getWriter().write("Hello, world!");
              doPost(request,response);
       }  
     
       /* (non-Java-doc)
        * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              String username = request.getParameter("username");
              String password = request.getParameter("password");
            
              request.setAttribute("USER", username);
              request.setAttribute("PASSWORD", password);
            
              ServletContext context = getServletContext();
            
              System.out.println("Redirecting to" + target);
              RequestDispatcher dispatcher = context.getRequestDispatcher(target);
              dispatcher.forward(request,response);
       }                   
}

2.輸入介面:HelloWorld\login.jsp。一個文字框/text、密碼框/password、按鈕/input type="Submit"。複製貼上.沒有排版,反正不是現在要看的。

action="HelloServlet",web.xml中取的某個名字,方便起見,採用類名.

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>helloapp</title>
</head>
<body>
<br>
<form name="loginForm" method="post" action="HelloServlet">
<table>
<tr>
<td><div align="right">User Name:</div></td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td><div align="right">Password:</div></td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><<input type="Submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
 
</body>
</html>
3.輸出使用頁面:HelloWorld\hello.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>helloapp</title>
</head>
<body>
<b>Welcome:<%= request.getAttribute("USER") %></b>
</body>
</html>
4.index新增超級連結到login頁面

       <p><a href="login.jsp?language=English">login</a>     

6.web.xml配置servlet
    <servlet>
      <servlet-name>HelloServlet</servlet-name>
      <servlet-class>yqj2065.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/abc</url-pattern>
    </servlet-mapping>
在瀏覽器中直接輸入http://localhost:8080/HelloWorld/abc。之後,abc改回HelloServlet。

4.最後的任務

把Java web執行需要做的事情,變成2個超級連結。