How to get the value selected by the Select label in the JSP __jsp

Source: Internet
Author: User


Today, write code encountered a problem, in the JSP page through the form of the Select Tag Value, form submitted to its own page, and then through the Request.getparameter () method to get the value



The test code is as follows (file name: testselect.jsp):





<% @ page language = "java" import = "java.util. *" pageEncoding = "GB18030"%>
<%
request.setCharacterEncoding ("GB18030"); // Add this sentence to solve
String path = request.getContextPath ();
String basePath = request.getScheme () + ": //"
+ request.getServerName () + ":" + request.getServerPort ()
+ path + "/";
// Array that stores the corresponding value of the menu
ArrayList nu = new ArrayList ();
nu.add ("one");
nu.add ("two");
nu.add ("three");
%>
<! DOCTYPE HTML PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN">
<html>
<head>
<base href = "<% = basePath%>">
<title> My JSP 'testselect.jsp' starting page </ title>
</ head>
<body>
Use request.getParameter ("number") method to get the value selected by the drop-down box
<form method = post action = "testselect.jsp"> <!-Submit to yourself->
<select name = number>
<%
for (int i = 0; i <nu.size (); i ++) {
out.print ("<option>" + nu.get (i) + "</ option>");
}
%>
</ select>
<input type = "submit" value = "submit" name = "submit">
</ form>
</ body>
<%
// Get the submitted number and display
String n = (String) request.getParameter ("number");
out.print ("The selected value is:" + n);
%>
</ html> 

Run the interface:











Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.