Ta_vish 2017-10-01 03:51 采纳率: 100%
浏览 2114
已采纳

关于隐藏表单提交后获取不到值的问题

 package com.bit;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueryUserTemp extends HttpServlet {

    private static final long serialVersionUID = -3953175627432572010L;

    private static final int pageLevel = 1;

    public void doGet(HttpServletRequest req, HttpServletResponse res) {
        if (!VerifyUser.verify(req, res, pageLevel)) {
            return;
        }

        String username = req.getParameter("username");
        String queryType = req.getParameter("qType");
        int pageNow = 1;
        try {
            String toPage = req.getParameter("toPage");
            if (toPage != null) {
                pageNow = Integer.parseInt(toPage);
            }
        } catch (Exception e) {

        }
        int grd = 0;

        System.out.println();
        String testString = req.getParameter("testString");
        System.out.println(username + " username");
        System.out.println(queryType + " queryType");
        System.out.println(pageNow + " pageNow");
        System.out.println(testString + " testString");
        System.out.println();

        PrintWriter pw = null;
        UserBeanCl ubc = new UserBeanCl();
        ArrayList<UserBean> al = new ArrayList<>();
        res.setContentType("text/html;charset=utf-8");

        try {
            pw = res.getWriter();
            pw.println("<html>");
            pw.println("<head>");
            pw.println("<link rel='stylesheet' type='text/css' href='css/theme.css'>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<img src=imgs/logo.png id='logo'><br>");
            pw.println("<h1>Query User</h1>");
            pw.println("<form action=queryusertemp method=post>");
            pw.println(
                    "<label for='username'>username:&nbsp;<input type=text id='username' name=username></label><br><br><br>");
            pw.println(
                    "<label for='fuzzy'>Fuzzy&nbsp;<input type=radio id='fuzzy' name=qType value=fuz checked></label>");
            pw.println(
                    "<label for='precise'>Precise&nbsp;<input type=radio id='precise' name=qType value=pre></label><br>");
            pw.println("<br><br><input type=submit value=Query><br><br>");

            if (username != null && queryType != null) {
                if (username.equals("")) {
                    try {
                        res.sendRedirect("err?operation=Query");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    String ugrd = (String) req.getSession(true).getAttribute("ugrd");
                    grd = Integer.parseInt(ugrd);

                    al = ubc.queryUserByName(username, queryType, pageNow);

                    if (al.size() == 0) {

                        pw.println("<h2><em>No Result.</em></h2>");

                    } else {
                        pw.println("<h2>Query Result:</h2>");
                        pw.println("<table border=1 id='table'>");
                        pw.println("<tr bgcolor='pink'><th>id</th><th>name</th>");
                        if (grd == 10) {
                            pw.println("<th>pwd</th>");
                        }
                        pw.println("<th>email</th><th>grade</th></tr>");
                        Iterator<UserBean> it = al.iterator();
                        while (it.hasNext()) {
                            UserBean ub = it.next();
                            pw.println("<tr>");
                            pw.println("<td>" + ub.getUserId() + "</td>");
                            pw.println("<td>" + ub.getUsername() + "</td>");
                            if (grd == 10) {
                                pw.println("<td>" + ub.getPasswd() + "</td>");
                            }
                            pw.println("<td>" + ub.getEmail() + "</td>");
                            pw.println("<td>" + ub.getGrade() + "</td>");
                            pw.println("</tr>");
                        }
                        pw.println("</table><br>");
                        pw.println("Current page is " + pageNow + "<br><br>");

                        int pageCount = ubc.getPageCount();

                        if (pageNow != 1)
                            pw.println("<a href=queryusertemp?toPage=" + (pageNow - 1) + "&username=" + username
                                    + "&qType=" + queryType + ">" + "Previous" + "</a>");
                        int maxPage = pageNow + 4;
                        if (maxPage <= pageCount) {
                            for (int i = pageNow; i <= maxPage; i++) {
                                pw.println("<a href=queryusertemp?toPage=" + i + "&username=" + username + "&qType="
                                        + queryType + ">" + i + "</a>");
                            }
                        } else {
                            for (int i = pageNow; i <= pageCount; i++) {
                                pw.println("<a href=queryusertemp?toPage=" + i + "&username=" + username + "&qType="
                                        + queryType + ">" + i + "</a>");
                            }
                        }
                        if (pageNow != pageCount)
                            pw.println("<a href=queryusertemp?toPage=" + (pageNow + 1) + "&username=" + username
                                    + "&qType=" + queryType + ">" + "Next" + "</a>");

                        pw.println("<br><form action=queryusertemp>");
                        pw.println(
                                "<label for='goPage'>Skip to:<input type=number name=toPage id='goPage' min='1' max="
                                        + pageCount + "></label>");
                        pw.println("<input type=hidden name=qType value=" + queryType + ">");  
                        pw.println("<input type=hidden name=username value=" + username + ">");  
                        pw.println("<input type=hidden name=testString value=" + username + ">");  

                        pw.println("<input type=submit value=GO>");
                        pw.println("</form>");

                    }
                }

            }
            pw.println("<br><br><a href=main>Return to Main Page</a>");
            pw.println("<br><br><img id='flower' src=imgs/flower.png>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) {
        doGet(req, res);
    }
}

最下面的隐藏表单部分提交了toPage、qType、username、testString给当前页面。但是在测试部分(控制台输出)却只能接收到toPage、qType和testString,username的值变成了 ""。

求解。

第一次访问该servlet

使用er2作为关键字进行检索

使用隐藏表单提交的信息进行检索

  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2017-10-01 10:01
    关注

    首先,确认一下你隐藏表单域的name属性跟后台的接收参数名称一致。
    其次,浏览器的F12看下表单提交时的参数都是什么。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动