1.編寫servlet程序,在頁面上輸出HelloWorld
要求:
(1)創建項目之後new—class,自行編寫servlet程序,覆蓋doGet及doPost方法
(2)在web.xml中配置<servlet>及<servlet-mapping>
2.編寫servlet程序讀取表單中的數據
[html] <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form method="post" action="ServletTest2">
<input type="text" name="text" />
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form method="post" action="ServletTest2">
<input type="text" name="text" />
<input type="submit" value="提交">
</form>
</body>
</html>
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>helloworld</servlet-name>
<servlet-class>com.mars.ServletTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/mars/firstTest</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletTest2</servlet-name>
<servlet-class>com.mars.ServletTest2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest2</servlet-name>
<url-pattern>/ServletTest2</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>helloworld</servlet-name>
<servlet-class>com.mars.ServletTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/mars/firstTest</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletTest2</servlet-name>
<servlet-class>com.mars.ServletTest2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest2</servlet-name>
<url-pattern>/ServletTest2</url-pattern>
</servlet-mapping>
</web-app>
[java]
package com.mars;
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;
public class ServletTest extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("<html><head></head><body>hello world ~~mars</body></html>");
out.close();
}
/* protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("<html><head></head><body>hello world ~~mars</body></html>");
out.close();
}*/
}
package com.mars;
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;
public class ServletTest extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("<html><head></head><body>hello world ~~mars</body></html>");
out.close();
}
/* protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("<html><head></head><body>hello world ~~mars</body></html>");
out.close();
}*/
}
要求:
(1) 編寫JSP頁面,創建表單,表單中包含“文本框”及“提交”按鈕
(2) 編寫servlet程序,在程序中讀取出用戶在表單中輸入的內容,並將其顯示在頁面上
(3) 創建servlet 程序時,可采用MyEclipse中直接new—servlet的方式
[java]
package com.mars;
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;
public class ServletTest2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
System.out.println("in Get");
out.println("<HTML>");
out.println(" <HEAD></HEAD>");
out.println(" <BODY>");
out.println("從jsp頁面,獲取信息的示例");
out.println(request.getParameter("text"));
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("in Post");
doGet(request,response);
}
}
摘自 Mars學IT