gpt4 book ai didi

java - 使用servlet处理时如何使用javascript设置隐藏变量的值?

转载 作者:行者123 更新时间:2023-11-28 20:59:18 25 4
gpt4 key购买 nike

嗨,这就是我通过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 中。

请帮我找出答案。

最佳答案

你应该这样做:

funtiontype =request.getParameter("funtiontype");
acctcode = request.getParameter("acctcode");

关于java - 使用servlet处理时如何使用javascript设置隐藏变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11429412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com