如何在使用servlet处理时使用javascript设置隐藏变量的值
How to set values for hidden variable using javascript when processing using servlet?
嗨,这就是我如何通过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"> </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%"> </td>
<td width="10%" align="center"> </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"> </div>
</div>
</div>
<div id="footer">
<p>© 2012 <a href="#">slingmedia</a> | ACCT <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");
试试这个。。
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");
相关文章:
- 在jquery中为显示/隐藏设置cookie
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 如何使用javascript将值引导下拉列表设置为隐藏值asp.net
- 通过ng-if设置隐藏文本框的值
- 在提交时设置表单隐藏值
- 如何使用 JavaScriptJQuery 将隐藏输入标签的值与选择标签的选定选项的值进行设置
- 获取下一个或上一个隐藏元素的高度,以设置父元素的高度.jquery.
- 如何在代码镜像编辑器中设置隐藏值
- <asp.隐藏字段>即使在javascript中设置了值,codeehind中的值也会变为空
- getElementById().value未设置隐藏输入
- 仅当onclick和隐藏消息时设置cookie
- 如何在隐藏的文本框上设置焦点
- 根据两个下拉菜单的选择设置隐藏值
- jQuery-是否可以使用jQuery在隐藏的文本框中设置值
- 设置隐藏值后重新加载页面
- 在代码隐藏中从DataTable设置javascript数组
- Wistia Javascript API-设置视频泡沫真实和隐藏社交按钮
- 如何在使用servlet处理时使用javascript设置隐藏变量的值
- 从 C# 代码隐藏设置传单缩放区域
- 从JavaScript或代码隐藏设置AjaxFileUpload控件的te值