无法读取servlet中的表单字段 [英] Unable to read form field in servlet

查看:120
本文介绍了无法读取servlet中的表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我对servlet环境很陌生。在这里,我试图将表单发布到我的servlet,如下所示:

Hey I am quite new to servlet environment. Here I am trying to post a form to my servlet with something like this:

<form action="OnlineExam?q=saveQuestion" method="post" enctype="multipart/form-data">
        <fieldset>
        <legend>Question</legend>
        <textarea class="questionArea" id="question" name="question">Enter Question.</textarea>
        <br class="clearFormatting"/>
        Attach File<input type="file" name="file" />

        <input class="optionsInput" value="Option A" name="A" onfocus = "clearValues('A')" onblur = "setValues('A')"/>
        <br class="clearFormatting"/>

        <input class="optionsInput" value="Option B" name="B" onfocus = "clearValues('B')" onblur = "setValues('B')"/>
        <br class="clearFormatting"/>

        <input class="optionsInput" value="Option C" name="C" onfocus = "clearValues('C')" onblur = "setValues('C')"/>
        <br class="clearFormatting"/>

        <input class="optionsInput" value="Option D" name="D" onfocus = "clearValues('D')" onblur = "setValues('D')"/>
        <br/>

        <input type="submit" value="Save" />
        <input type="reset" value="Cancel" />
        <button style="display: none" onclick="return deleteQuestion()" >Delete</button>
        </fieldset>
        </form>

servlet是这样的:

And the servlet is something like this:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("q").equals("saveQuestion")){           
                saveQuestion(request);
            }
}

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

public void saveQuestion(HttpServletRequest request){
                 Enumeration enum = request.getParameterNames();
                 while (enum.hasMoreElements()) {
                   String pName = (String) enum.nextElement();
                   String[] pValues = request.getParameterValues(pName);
                   System.out.print("<b>"+pName + "</b>: ");
                   for (int i=0;i<pValues.length;i++) {
                      System.out.print(pValues[i]);
                   }
                   out.print("<br>");
                 }
}

但它只打印q参数而不打印其他参数表单字段。

But it is printing only the q parameter not the other form fields.

我还试图用 request.getParameter(question)来获取它们但这是也不行。所以我哪里出错了。实际上我来自PHP背景,最近开始用java编写,所以请帮忙。

I also tried to get them with the request.getParameter("question") but this was also not working. So where i am going wrong. actually i am from PHP background and recently started coding in java so please help.

提前致谢

推荐答案

当您使用 enctype =multipart / form-data时,您无法像通常那样访问请求参数[即的request.getParameter( 问题)]。您必须使用 MultipartRequest 对象。

When you use enctype="multipart/form-data" you can not access request parameter as you normally do[that is request.getParameter("question")]. You have to use MultipartRequest object.

您还可以在POST中提交表单,然后在servlet中将其重定向到doGet。为什么这样?为什么不直接在表单提交中使用GET作为方法。

And also you submit form in POST and then in servlet you redirect it to doGet. Why so? Why don't directly use GET as a method in form submit.

演示使用MultipartRequest:

Demo to use MultipartRequest:

String ph="images\\";
MultipartRequest req=new MultipartRequest(request, ph);
String question=req.getParameter("question");
System.out.println("Question: "+question);

这篇关于无法读取servlet中的表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆