将字符串值从JSP传递给Servlet时出现空值

时间:2017-06-02 10:33:35

标签: java jsp servlets

上传jsp文件......从这里开始..

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Upload</title>
</head>
    <body>
        <h1>Hello World!</h1>

        <form id="myservlet" method="POST" action="uploadServlet2" enctype="multipart/form-data" >
            User Name:<input type="text" name="userName" /><br/><br/>
                      <input type="submit" value="submit"/>



        </form>
    </body>
</html>

Servlet类文件

package net.codejava.servlet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * Servlet implementation class uploadServlet2
 */
@WebServlet("/uploadServlet2")

public class uploadServlet2 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
//      response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    String s=request.getParameter("userName");
        System.out.println("Print " +s);


    }

}

用户名值不显示并获取空值.... Plz帮帮我.. 服务器版本是否有任何问题..我尝试和谷歌搜索但不工作.. 使用apache 8.5 server和servlet 3.0

1 个答案:

答案 0 :(得分:0)

如果您没有上传文件,请删除enctype

<form id="myservlet" method="POST" action="uploadServlet2" >