javaWeb form表单提交数据

Java servlet

package com.my.servlet;

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;

import com.sun.jmx.snmp.Enumerated;

/**
 * Servlet implementation class FormRequestServlet
 */
@WebServlet("/FormRequestServlet")
public class FormRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FormRequestServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//在页面直接打印jsp文件
		response.setContentType("text/html;charset=UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String[] channel = request.getParameterValues("channel");
		Enumeration elem = request.getParameterNames();
		PrintWriter out = response.getWriter();
		out.println("

用户:"+username+"

"); out.println("

密码:"+password+"

"); out.println("

信息来源:"); for(String c:channel) { out.println(c+" "); } out.println("

所有客户请求的参数是:"); while(elem.hasMoreElements()) { out.println(elem.nextElement()+" "); } out.println("

"); out.flush(); out.close(); } }

jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




Insert title here


	

用户名:

密码:

信息来源: 网络 报纸 亲友

xml配置:



  mytest2
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
	HelloServlet
	HelloServlet
	com.my.servlet.HelloServlet
	
		username
		qst
	
	0
	true



	HelloServlet
	/

  

 

你可能感兴趣的:(JavaWeb)