首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

一个很奇怪的有关问题!如果能解决 不够分您开腔

2012-01-22 
一个很奇怪的问题!如果能解决 不够分您开腔!我有一个.html页面.把里面的值提交给一个servlet 具体代码如下

一个很奇怪的问题!如果能解决 不够分您开腔!
我有一个.html页面.把里面的值提交给一个servlet 具体代码如下:
package   model;

import   java.io.*;
import   javax.servlet.http.*;
import   java.util.*;
import   java.sql.*;

import   javax.servlet.*;


public   class   dcservlet   extends   HttpServlet   {
                private   static   final   String   CONTENT_TYPE= "text/html;charset=GBK ";
                private   String   comeOutStr= " ";
                private   int   comeOutInt=0;
                private   static   Connection   con;
                private   Statement   stmt;
                  private   static   final   String   drivername= "org.gjt.mm.mysql   Driver ";
                  private   static   final   String   url= "jdbc:mysql://localhost/zqswork02?user=root&password=root&useUnicode=true&characterEncoding=gb2312 ";
                  public   static   Connection   getCon()   throws   Exception   {
                  try   {
                  Class.forName(drivername);
                  con=DriverManager.getConnection(url);
                  return   con;                  

}   catch   (SQLException   e)   {
System.err.println(e.getMessage());
throw   e;
}

}
                public   Statement   getStmtread()   {
                try   {
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
return   stmt;
}   catch   (Exception   e)   {
System.err.println(e.getMessage());
e.printStackTrace();
}
return   null;
}




public   void   doPost(HttpServletRequest   request,   HttpServletResponse   response)
throws   ServletException,   IOException   {

response.setContentType(CONTENT_TYPE);
PrintWriter   out   =   response.getWriter();
out.println( " <!DOCTYPE   HTML   PUBLIC   \ "-//W3C//DTD   HTML   4.01   Transitional//EN\ "> ");
out.println( " <HTML> ");
out.println( "     <HEAD> <TITLE> A   Servlet </TITLE> </HEAD> ");
out.println( "     <BODY> ");
out.print( "         This   is   ");

String   name=toStr((String)request.getParameter( "name "));
String   password=toStr((String)request.getParameter( "paswword "));
String   address=toStr((String)request.getParameter( "address "));
int   tel=toInt((String)request.getParameter( "tel "));


String   sql= "insert   into   zqsworktable2(name,password,address,tel)values( ' "+toChinese(name)+ " ', ' "+toChinese(password)+ " ', ' "+toChinese(address)+ " ', ' "+tel+ " ') ";
try   {
stmt=getStmtread();
int   i=stmt.executeUpdate(sql);
if   (i> 0)   {
out.println( "使用servlet更新成功 ");

}
}   catch   (Exception   e)   {

}
out.println( "     </BODY>   </html> ");


}
public   String   toStr(String   comelnStr){

if(comelnStr==null||comelnStr.equals( " ")){

return   comeOutStr= "no ";
}else{
return   comeOutStr.trim();
}
}
public   int   toInt(String   comeInInt){
try   {
comeOutInt=Integer.parseInt(comeInInt);
}   catch   (Exception   e)   {
    comeOutInt=0;
}
return   comeOutInt;
}
public   String   toChinese(String   str){

try   {
str=(new   String(str.getBytes( "iso-8859-1 "), "GB2312 "));
}   catch   (Exception   e)   {
return   str;
}
                    return   str;
}
}

--------------------------
<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html   xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "   />
<title> 无标题文档 </title>
</head>

<body>
<form   id= "form1 "   name= "form1 "   method= "post "   action= "dcservlet ">
    <p>                                               软件插入数据库 </p>
    <p> 姓   名:                                        
        <input   type= "text "   name= "name "   />
    </p>
    <p> 密   码:
        <input   type= "text "   name= "password "   />
    </p>
    <p> 地   址:  
        <input   type= "text "   name= "address "   />
    </p>
    <p> 联系电话:
        <input   type= "text "   name= "tel "   />
    </p>
    <p> &nbsp; </p>
    <p>
 
        <input   type= "submit "   name= "Submit "   value= "提交 "   />
 
 
        <input   type= "reset "   name= "Submit2 "   value= "重置 "   />


   
    </p>
   
</form>
</body>
</html>
------------------------------web.xml
<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<web-app   version= "2.4 "  
xmlns= "http://java.sun.com/xml/ns/j2ee "  
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "  
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee  
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
    <servlet>
        <description> This   is   the   description   of   my   J2EE   component </description>
        <display-name> This   is   the   display   name   of   my   J2EE   component </display-name>
        <servlet-name> dcservlet </servlet-name>
        <servlet-class> model.dcservlet </servlet-class>
    </servlet>
   


    <servlet-mapping>
        <servlet-name> dcservlet </servlet-name>
        <url-pattern> /servlet/dcservlet </url-pattern>
    </servlet-mapping>
   
  <welcome-file-list>
  <welcome-file>
                dcservelt.html
  </welcome-file>
  </welcome-file-list>
</web-app>

但运行出来一提交就出现这个问题:
HTTP   Status   404   -   /zqswork02/dcservlet

--------------------------------------------

type   Status   report

message   /zqswork02/dcservlet

description   The   requested   resource   (/zqswork02/dcservlet)   is   not   available.


--------------------------------------------

Apache   Tomcat/5.0.28

谁能告诉我怎么会事???????????????????如何解决!



[解决办法]
<servlet-mapping>
<servlet-name> dcservlet </servlet-name>
<url-pattern> /servlet/dcservlet </url-pattern>
</servlet-mapping>

---->
<servlet-mapping>
<servlet-name> dcservlet </servlet-name>
<url-pattern> /dcservlet </url-pattern>
</servlet-mapping>

调用的时候直接写dcservlet
[解决办法]
第一个问题显示的就是这样的错误,期待高手了
第二个.数据在存入数据库时 new String(strvalue.getBytes( "gb2312 "), "ISO8859_1 ");
在取出来时 new String(strvalue.getBytes( "ISO8859_1 "), "gb2312 ");

也许第一个错误就是由第二个错误引起的,猜测而已

热点排行