HttpServlet 简单表单示例

本文代码转载自

《Servlet、JSP和Spring MVC初学指南》

下面是一个简单的利用get进行输入表单页面显示,post进行
表单提交内容展示的实例

package main.ServletTest;

/**
 * Created by ehang on 2017/2/8.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name = "FormServlet",
        urlPatterns = {"/form"}
)
public class FormServlet extends HttpServlet {
    private static final long serialVersionUID = 54L;
    private static final String TITLE = "Order Form";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.println("" +
                TITLE +
                "" +
                "

" + TITLE + "

" + "
" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
Name:
Address:
Country:
Delivery MethodFirst Class" + "Second Class" + "
Shipping Instructions:" + "
 
Please send me the latest product catalog
 
" + "
" + ""); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response )throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("" + "" + TITLE + "" + "

" + TITLE + "

" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
Name:" + request.getParameter("name") + "
Address:" + request.getParameter("address") + "
Country:" + request.getParameter("country") + "
Shipping Instructions:"); String[] instructions = request.getParameterValues("instructions"); if(instructions != null){ for(String instruction: instructions){ writer.println(instruction + "
"); } } writer.println("
Delivery Method:" + request.getParameter("deliveryMethod") + "
Catalog Request:"); if(request.getParameter("catalogRequest") == null){ writer.println("No"); } else{ writer.println("Yes"); } writer.println( "
"); writer.println( "
" + "Debug Info
"); Enumeration parameterNames = request.getParameterNames(); while(parameterNames.hasMoreElements()){ String paramName = parameterNames.nextElement(); writer.println(paramName + ": "); String[] paramValues = request.getParameterValues(paramName); for(String paramValue: paramValues){ writer.println(paramValue + "
"); } } writer.println( "
" + ""); } }

使用input标签对应的name来通过request.getParameter得到提交值。

该例使用 annotation WebServlet urlPatterns映射,一般提到的映射方式为web.xml中的部署描述符配置方式。
后者较前者的优势在于提供了WebServlet所不支持的若干描述 如位于中可配置
该节点支持整数值作为输入,指定在程序启动时而非第一次调用时初始化servlet类,
当对多个servlet都配置了该节点时,不同的load-on-start节点值决定了载入的先后顺序,
基本上类似于对中间件指定载入顺序。
描述符配置的方式也可包含如servlet进行初始化信息。

如利用annotation WebServlet可如下指定servlet初始化参数
@WebServlet(
    name = "test",
    urlpatterns = {"/test"};
    initParams = {
        @WebInitParams(name = "admin", value = "SV")
    }
)
 
  
相应的部署描述符servlet配置为

    test
    SomeClass
    
        admin
        SV
    



    test
    /test



你可能感兴趣的:(网络编程)