0

嗨,这就是我如何通过 java 脚本设置值是隐藏变量,

function logintosystem(){
                document.forms["frmLogon"].funtiontype.value="logon";
                document.forms["frmLogon"].submit();
            }

这就是我的jsp页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = "", userName = "", message = "", userType = "";
    path = request.getContextPath();
     if(request.getAttribute("message")!=null) message =(String)request.getAttribute("message");
%>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="description" content="ACCT"/>
        <meta name="keywords" content="acct,accesscardcomparision" />
        <meta name="author" content="slingmedia" />
        <link rel="stylesheet" type="text/css" href="acct.css" title="andreas09" media="screen,projection" />
        <title>ACCT</title>
        <script>
            function logintosystem(){
                document.forms["frmLogon"].funtiontype.value="logon";
                document.forms["frmLogon"].submit();
            }

        </script>
    </head>
    <body>

        <div id="container">
            <div id="sitename">
                <h1>ACCT</h1>
                <h2>Access Card data comparision with Leave portal</h2>
            </div>
            <div id="mainmenu">
                <ul>
                    <%
                        if (userName.length() > 0 || !userName.equals(null) || !userName.equals("")) {
                    %>
                    <li><a href="#" class="current" >Welcome <%=userName%></a></li>
                    <%}%>
                    <%
                        if (userName.length() > 0 && userType.length() > 0) {
                    %>
                    <li><a href="#">Tempcard</a></li>
                    <%}%>
                    <%
                        if (userType.equals("HR")) {
                    %>
                    <li><a href="#">Report</a></li>
                    <%}%>
                    <%
                        if (userName.length() > 0 && userType.length() > 0) {
                    %>
                    <li><a href="#">Logout</a></li>
                    <%}%>
                </ul>
            </div>
            <div id="wrap">
                <div id="content">

                    <h1>Please Enter your code</h1>
                     <form name="frmLogon" id="frmLogon" action="LogonServlet" method="post">
                    <table align="center" border="0" cellpadding="0" cellspacing="0"  width="100%">
                        <tr align="center" valign="middle">
                            <td width="100%" align="center" valign="middle">
                                <table width="100%" align="left" border="0" cellspacing="0" cellpadding="2" >
                                    <tr>
                                        <td width="5%" align="left">&nbsp;</td>
                                        <td width="35%" align="right">ACCT Code</td>
                                        <td width="20%"><input type="text" name="acctcode" id="acctcode" class="inputBoxes" /></td>
                                        <td width="15%" align="left"><input type="button" class="submitButton" value="Logon" onclick="logintosystem();"/></td>
                                        <td width="15%">&nbsp;</td>
                                        <td width="10%" align="center">&nbsp;</td>
                                    </tr>
                                    <br></br>
                                </table>
                            </td>
                        </tr>
                    </table>
                         <input type="hidden" name="funtiontype" id="funtiontype" value=""/>
                 </form>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFF0">
                        <tr>
                            <td id="message"  align="center">
                                <b><font  color="brown" size="3"><%=message%></font></b>
                            </td>
                        </tr>
                    </table>
                    </td>
                    </tr>
                    </table>
                </div>
                <div class="clearingdiv">&nbsp;</div>
            </div>
        </div>

        <div id="footer">
            <p>&copy; 2012 <a href="#">slingmedia</a> | ACCT&nbsp;&nbsp;&nbsp;<a href="http://slingmedia.com/"></a></p>
        </div>
    </body>
</html>

我的servlet是:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.slingmeadia.notifier.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author anthony.savarimut
 */
public class LogonServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LogonServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet LogonServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       // processRequest(request, response);
        String funtiontype = "",acctcode="",pageRedirect="";

        if(request.getAttribute("funtiontype")!=null) funtiontype =(String)request.getAttribute("funtiontype");
        if(request.getAttribute("acctcode")!=null) acctcode =(String)request.getAttribute("acctcode");

        if(funtiontype.equals("logon")){
            request.setAttribute("message","Loggedon code is "+acctcode);
            pageRedirect="notifier/notifier.jsp";
        }else{
            request.setAttribute("message","loggedout code is "+acctcode);
            pageRedirect="index.jsp";
        }
        response.sendRedirect(pageRedirect);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        System.out.println("Called logon");
        doGet(request,response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

该页面正在提交,但我为隐藏变量设置的值没有出现在 servlet 中,而且我有一个文本框,该值在提交页面时也没有出现在 servlet 中。

请帮助我找出这一点。

4

2 回答 2

2

你应该做这个:

funtiontype =request.getParameter("funtiontype");
acctcode = request.getParameter("acctcode");
于 2012-07-11T09:30:00.290 回答
1

试试这个..

function logintosystem(){
                document.frmLogon.funtiontype.value="logon";
                document.frmLogon.submit();
            }

更新:

String funtionType = request.getAttribute("funtiontype") == null ? request.getParameter("funtiontype") : request.getAttribute("funtiontype");
String acctCode = request.getAttribute("acctcode") == null ? request.getParameter("acctcode") : request.getAttribute("acctcode");
于 2012-07-11T09:26:55.103 回答