当前位置:编程学习 > JS >>

servlet基础

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 {

补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,