表单代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form method="get" action="../servlet/GuestBook">
<div align="center">
<center><p>您的姓名:<input type="text" name="name" size="30"><br/>
您的E-MAIL<input type="text" name ="mail" size=30"></p>
</center></div>
<div align ="center">
<center><p>
<textarea name="content" rows="5" name="main" cols="30">
</textarea>
</p></center>
</div>
<div align ="center">
<center>
<p>
<input type="submit" value="确定" name="B1" >
<input type="reset" value="取消" name="B2">
<input type="button" value="浏览留言" name ="B3" onclick="window.location.href='../servlet/Watch'">
</p>
</center></div>
</form>
</body>
</html>GuestBook:import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.URL;
import java.sql.*;
public class GuestBook extends HttpServlet
{
Connection con;
public void init(ServletConfig conf) throws ServletException
{
super.init();//调用父类的初始化函数
String l="jdbc:odbc:wl";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(l,"sa","");
}
catch(Exception e )
{
//没有进行处理
}
}
public void doGet(HttpServletRequest request ,HttpServletResponse response)
throws IOException,ServletException
{
String name=request.getParameter("name");
String name1= new String(name.getBytes("iso8859-1"));
String email=request.getParameter("email");
String email1= new String(email.getBytes("iso8859-1"));String content=request.getParameter("content");
String content1= new String(content.getBytes("iso8859-1"));
java.util.Date to_day = new java.util.Date();
String date_string=to_day.toString();
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>"); try
{
Statement stmt =con.createStatement();
String sql="insert into stud values('"+name1+"','"+email1+"','"+content1+"','"+date_string+"')";
stmt.executeUpdate(sql);
con.close();
out.println("<title>您已经成功添加完信息</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>您的意见已经成功输入数据中!谢谢您的支持!</h1>");
}
catch(SQLException ex)
{
while(ex!=null)
{
out.println("数据库异常被捕获了");
out.println(ex.getSQLState());
out.println(ex.getMessage());
out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}
out.println("</body>");
out.println("</html>");
}
}
第一个变量name1取得的是中文,可email1,content1显示的是????,怎么解决?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form method="get" action="../servlet/GuestBook">
<div align="center">
<center><p>您的姓名:<input type="text" name="name" size="30"><br/>
您的E-MAIL<input type="text" name ="mail" size=30"></p>
</center></div>
<div align ="center">
<center><p>
<textarea name="content" rows="5" name="main" cols="30">
</textarea>
</p></center>
</div>
<div align ="center">
<center>
<p>
<input type="submit" value="确定" name="B1" >
<input type="reset" value="取消" name="B2">
<input type="button" value="浏览留言" name ="B3" onclick="window.location.href='../servlet/Watch'">
</p>
</center></div>
</form>
</body>
</html>GuestBook:import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.URL;
import java.sql.*;
public class GuestBook extends HttpServlet
{
Connection con;
public void init(ServletConfig conf) throws ServletException
{
super.init();//调用父类的初始化函数
String l="jdbc:odbc:wl";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(l,"sa","");
}
catch(Exception e )
{
//没有进行处理
}
}
public void doGet(HttpServletRequest request ,HttpServletResponse response)
throws IOException,ServletException
{
String name=request.getParameter("name");
String name1= new String(name.getBytes("iso8859-1"));
String email=request.getParameter("email");
String email1= new String(email.getBytes("iso8859-1"));String content=request.getParameter("content");
String content1= new String(content.getBytes("iso8859-1"));
java.util.Date to_day = new java.util.Date();
String date_string=to_day.toString();
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>"); try
{
Statement stmt =con.createStatement();
String sql="insert into stud values('"+name1+"','"+email1+"','"+content1+"','"+date_string+"')";
stmt.executeUpdate(sql);
con.close();
out.println("<title>您已经成功添加完信息</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>您的意见已经成功输入数据中!谢谢您的支持!</h1>");
}
catch(SQLException ex)
{
while(ex!=null)
{
out.println("数据库异常被捕获了");
out.println(ex.getSQLState());
out.println(ex.getMessage());
out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}
out.println("</body>");
out.println("</html>");
}
}
第一个变量name1取得的是中文,可email1,content1显示的是????,怎么解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货