为什么我用提交按钮提交会是这样,名字输入的中文,下面的爱好后面是啥?

来源:3-8 request对象(下)

黑豆0

2016-02-28 08:07


User: ??
frivious:[Ljava.lang.String;@24ca1465
read
swam
password:123 

我的代码

request.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'request.jsp' starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

  </head>

  <body>

  

  <h1>inner request</h1>

  <%

  

  String expr = new String(request.getParameter("Username").getBytes("ISO-8859-1"),"UTF-8"); 

  request.setCharacterEncoding("utf-8");

  request.setAttribute("password", "123");


  %>

  User: <%=expr %><br>

  frivious:<%=request.getParameterValues("frivious") %><br>

  <%

  if(request.getParameterValues("frivious")!=null){

  String[] frivious=request.getParameterValues("frivious");

  for(int i=0;i<frivious.length;i++){

  out.print(frivious[i]+"<br>");

  }

  }

   %>

   password:<%=request.getAttribute("password")%>

  </body>

</html>

=====================================

=============req.jsp:========================

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'req.jsp' starting page</title>

    

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->


  </head>

  

  <body>

   <form name="req" action="request.jsp" method="get">

   <table>

   <tr>

   <td>Username</td>

   <td><input type="text" name="Username"></td>

   </tr>

   <tr>

   <td>fravious</td>

   <td><input type="checkbox" name="frivious" value="read">read

   <input type="checkbox" name="frivious" value="music">music

   <input type="checkbox" name="frivious" value="swam">swam

   <input type="checkbox" name="frivious" value="foot">foot

    

   </td>

   </tr>

   <tr>

   <td colspan="2"><input type="submit" value="value"></td>

   </table>

  

   </form>

   <br>

   <br>

    <a href="request.jsp?Username=王五">test of the URl</a>

  </body>

</html>


写回答 关注

1回答

  • 慕函数5563272
    2016-02-28 11:14:34
    已采纳

    亲,我觉得你的favorites(frivious)输出的结果是“[Ljava.lang.String;@24ca1465”的原因是因为你直接输出了一个数组,这样是不正确的,你的这种行为类似于out.print(s),其中s是数组,二对于s你不应该直接输出它,而是应该依次输出其中的元素。

    黑豆0

    非常感谢!

    2016-02-28 15:08:44

    共 1 条回复 >

JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248277 学习 · 3071 问题

查看课程

相似问题