Download (1-1) TestServlet.java ,存在com.abc.control 中. package com.abc

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Transcript
第 12 講
(1-1) TestServlet.java ,存在 com.abc.control 中.
package com.abc.control;
import
import
import
import
import
java.io.IOException;
javax.servlet.ServletException;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
java.io.*;
public class TestServlet extends javax.servlet.http.HttpServlet {
static final long serialVersionUID = 1L;
public TestServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("Servlet 的測試輸出");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
(1-2) Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
…
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.abc.control.TestServlet</servlet-class>
…
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/ts</url-pattern>
</servlet-mapping>
…
</web-app>
第 12 講
(2-1) index.hmtl
<!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=UTF-8">
<title>參數傳遞測試 </title> </head>
<body>
<form action = "rs" method="get">
客戶編號:<input type="text" name="cusNo">
<input type="submit">
</form>
</body>
</html>
(2-2) RequestServlet.java ,存在 com.abc.control 中.
package com.abc.control;
import
import
import
import
javax.servlet.ServletException;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
java.io.*;
public class RequestServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
static final long serialVersionUID = 1L;
public RequestServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String cusNo = request.getParameter("cusNo");
out.println("你的輸入編號:" + cusNo);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
第 12 講
(3-1) SessionServlet.java ,存在com.abc.control中.
package com.abc.control;
import
import
import
import
import
java.io.IOException;
javax.servlet.ServletException;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
java.io.*;
public class SessionServlet extends javax.servlet.http.HttpServlet {
static final long serialVersionUID = 1L;
public SessionServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String c2 = (String)request.getAttribute("cusNo");
out.println("request中的參數:" + c2);
out.println("<br>");
String c3 = (String)request.getSession().getAttribute("cusNo");
out.println("session中的參數:" + c3);
out.println("<br>");
String c4=(String)this.getServletContext().getAttribute("cusNo");
out.println("application中的參數:" + c4);
out.println("<br>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
(3-2) J01.jsp
<%@ page language="java" contentType="text/html; charset=BIG5"
pageEncoding="BIG5"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
pageContext.setAttribute("cusNo", "1111");
request.setAttribute("cusNo", "2222");
session.setAttribute("cusNo", "3333");
application.setAttribute("cusNo", "4444");
System.out.println(pageContext.getAttribute("cusNo"));
//(1)使用sendRedirect, Web容器將接收新的請求, 建立了新的request及response給
//
SessionServlet. 在瀏覽器的網址部分會顯示「/ss」.
response.sendRedirect("ss");
//(2)使用forward, Web容器不會收到新的請求,因此SessionServlet將使用原Web容器建立給J01的
//
request及response物件, 且瀏覽器的網址部分會顯示「/J01.jsp」.
//request.getRequestDispatcher("ss").forward(request, response);
%>
Similar