Topic: 求救!关于参数传递的问题。。。。。。。

  Print this page

1.求救!关于参数传递的问题。。。。。。。 Copy to clipboard
Posted by: snow_lxj
Posted on: 2005-12-01 21:23

我在书上练习一个实例,代码如下:当categoryJSP.jsp中的newsCategory传递到titleJSP.jsp中时,输出乱码,不知道是怎么回事,还望各位大虾指点,小弟感激不尽,送上一百二十万分的感谢,!!!
categoryJSP.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.URL" %>
<html>
<head>
<title>
新闻分类
</title>
</head>
<body>
<center>
<form action="titleJSP.jsp" method="post">
<%
String url="jdbc:odbc:news";
String query="select distinct newsCategory from news";
try{  
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn=DriverManager.getConnection(url);  
  Statement stmt=conn.createStatement();
  ResultSet rs=stmt.executeQuery(query);
  out.println("<p>新闻种类如下:</p>");
  out.println("<u1 type=\"square\">");
  while(rs.next()){
  String newsCategory=rs.getString("newsCategory");
  out.println("<li>");
  //写入超级链接"\"为转义符
  out.println("<a href=\"titleJSP.jsp?newsCategory="+newsCategory+"\">");
  out.println(newsCategory);
  out.println("</a>");
  out.println("</li>");
  }
  out.println("</ul>");
}catch(Exception e){
out.println(e.getMessage());
  e.printStackTrace();
}
%>
</form>
</center>
</body>
</html>

titleJSP.jsp

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>新闻标题:</title>
</head>
<body>
<center>

<%
request.setCharacterEncoding("gb2312");
String newsCategory=request.getParameter("newsCategory");
out.print(newsCategory);
try{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn=DriverManager.getConnection("jdbc:odbc:news");
  Statement stmt=conn.createStatement();

  ResultSet rs=stmt.executeQuery("select newsTitle from news");
  out.println("<p>新闻标题如下:</p>");  
  out.println("<ul type=\"square\">");
  while(rs.next()){
  //取得新闻的标题名称
  String newsTitle=rs.getString("newsTitle");
  out.println("<li>");
  //写入超级链接
  out.println("<a href=\"newsJSP.jsp?newsTitle="+newsTitle+"\">");
  out.println(newsTitle);
  out.println("</a>");
  out.println("</li>");
  }
  out.println("</ul>");
  out.println(newsCategory);
}catch(Exception e){
out.println(e.getMessage());
  e.printStackTrace();
}
%>
</center>
</body>
</html>

newsJSP.jsp

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>课件内容</title>
</head>
<body>
<center>
<%
String url="jdbc:odbc:news";
//request.setCharacterEncoding("gb2312");
String newsTitle=request.getParameter("newsTitle");
out.print(newsTitle);
out.print("hello");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select newsContent from news where newsTitle="+"'"+newsTitle+"'");
while(rs.next()){
  String newsContent=rs.getString("newsContent");
  out.println("<p>"+newsTitle+"</p>");
  out.println("<br>");
  out.println("<p>"+newsContent+"</p>");
  }
}catch(Exception e){
out.print(e.getMessage());
  e.printStackTrace();
}
%>
</center>
</body>
</html>

2.Re:求救!关于参数传递的问题。。。。。。。 [Re: snow_lxj] Copy to clipboard
Posted by: ntshenwh
Posted on: 2005-12-04 17:07

hi,
step1 :
import="java.io.*"
step 2:
try
{

  out.println(new String(request.getParameter("newsCategory").getBytes("ISO8859_1"),"GBK"));

}
catch( UnsupportedEncodingException e)
{
  e.printStackTrace();  
}



you may refer to “http://www.cnsdn.com.cn/inc/show.asp?id=2196”

3.Re:求救!关于参数传递的问题。。。。。。。 [Re: snow_lxj] Copy to clipboard
Posted by: snow_lxj
Posted on: 2005-12-05 19:41

谢谢高手了
我试试

4.Re:求救!关于参数传递的问题。。。。。。。 [Re: snow_lxj] Copy to clipboard
Posted by: snow_lxj
Posted on: 2005-12-05 20:30

我将上面的代码加到了titleCategory.jsp中out.println(newsCategory)处,并注销了原代码中的两行,结果不行,不知是怎么回事。
怎么还是不行呢?错误信息:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 11 in the jsp file: /news/titleJSP.jsp
Generated servlet error:
newsCategory cannot be resolved

An error occurred at line: 11 in the jsp file: /news/titleJSP.jsp
Generated servlet error:
newsCategory cannot be resolved

  org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
  org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
  org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
  org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
  org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
  org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
  org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

5.Re:求救!关于参数传递的问题。。。。。。。 [Re: snow_lxj] Copy to clipboard
Posted by: ntshenwh
Posted on: 2005-12-06 08:20

titleJSP.jsp

<%@ page contentType="text/html;charset=gb2312" import="java.io.*,java.sql.*" %>
<html>
<head>
<title>新闻标题:</title>
</head>
<body>
<center>

<%
//request.setCharacterEncoding("gb2312");

// get the param's value
String newsCategory=request.getParameter("newsCategory");
String result = null;

try
{
// convert params's value
result = new String(newsCategory.getBytes("ISO8859_1"),"GBK");
out.println(result);
}
catch( UnsupportedEncodingException e)
{
e.printStackTrace();  
}

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:news");
Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select newsTitle from news");
out.println("<p>新闻标题如下:</p>");
out.println("<ul type=\"square\">");
while(rs.next()){
//取得新闻的标题名称
String newsTitle=rs.getString("newsTitle");
out.println("<li>");
//写入超级链接
out.println("<a href=\"newsJSP.jsp?newsTitle="+newsTitle+"\">");
out.println(newsTitle);
out.println("</a>");
out.println("</li>");
}
out.println("</ul>");
out.println(newsCategory);
}catch(Exception e){
out.println(e.getMessage());
e.printStackTrace();
}
%>
</center>
</body>
</html>

----------------------------------------------------------------
please try it according to above code

good luck


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923