| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2492 人关注过本帖
标题:关于servlet的问题
只看楼主 加入收藏
cx521007
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2007-5-28
收藏
 问题点数:0 回复次数:10 
关于servlet的问题
掉弟最近刚开始学servlet想问问这么写那里不对

package
import
import
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class login extends HttpServlet {
    private String forname;
    private String url;
    private String user;
    private String pwd;
 /**
  * Constructor of the object.
  */
 public login() {
  super();
 }
 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }
 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  try{
   Class.forName(forname);
   Connection conn=DriverManager.getConnection(url,user,pwd);
   Statement st=conn.createStatement();
   String sql="SELECT COUNT(*) FROM luntan";
   ResultSet rt=st.executeQuery(sql);
   if(rt.next()){
    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("Welcome:"+request.getParameter("username"));
          out.println("  </BODY>");
          out.println("</HTML>");
          out.flush();
          out.close();
   }else{
    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("用户名或密码错误,请重新输入");
          out.println("  </BODY>");
          out.println("</HTML>");
          out.flush();
          out.close();
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
        doGet(request,response);
 }
 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
 public void init() throws ServletException {
  forname=getInitParameter("forname");
  url=getInitParameter("url");
  user=getInitParameter("user");
  pwd=getInitParameter("pwd");
 }
}
搜索更多相关主题的帖子: servlet import String java private 
2007-12-06 02:07
cx521007
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2007-5-28
收藏
得分:0 
这里是XML配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.
<web-app>
  <servlet>
    <servlet-name>login</servlet-name>
    <display-name>This is the display name of my J2EE component</display-name>
    <description>This is the description of my J2EE component</description>
    <servlet-class>
  </servlet>
  <init-param>
            <param-name>forname</param-name>
            <param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
     </init-param>
    <init-param>
            <param-name>url</param-name>
            <param-value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=luntan</param-value>
    </init-param>
    <init-param>
            <param-name>user</param-name>
            <param-value>sa</param-value>
    </init-param>
    <init-param>
            <param-name>pwd</param-name>
            <param-value>sa</param-value>
    </init-param>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/servlet/login</url-pattern>
  </servlet-mapping>

</web-app>

2007-12-06 02:07
doughty
Rank: 1
等 级:新手上路
帖 子:372
专家分:0
注 册:2007-10-18
收藏
得分:0 
有报异常没...有就贴出来看看
2007-12-06 08:33
scdmike
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-1-12
收藏
得分:0 
我试过这段代码应该没有问题,那你到底是什么问题呢?
2007-12-06 10:10
cx521007
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2007-5-28
收藏
得分:0 
从控制台上看到的错误,
java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at (login.java:43)
    at (login.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)

2007-12-07 12:19
scdmike
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-1-12
收藏
得分:0 
login.java:43是哪一行啊?我确定程序代码没有问题,你应该看看数据库有没有问题。也就是连接数据库,还有就是连接数据库的包导入了没有?
2007-12-07 14:38
cx521007
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2007-5-28
收藏
得分:0 
数据库我测试过没有错啊我用别的程序测试过一切都是正常显示
可用到这个servlet中就出现了错误进入到servlet生成的页面的时候什么都不显示也没报错在eclipse的控制台里边就显示上边哪个错误

2007-12-07 17:23
风月_无边
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:60
帖 子:2960
专家分:652
注 册:2007-1-19
收藏
得分:0 
應該是連接數據庫出了問題﹗﹗

我的网站 http://www.
2007-12-07 17:43
喃瑟
Rank: 1
等 级:新手上路
威 望:1
帖 子:472
专家分:0
注 册:2007-1-25
收藏
得分:0 
没有数据,看看你的数据库表对不对

光与影的1/2
2007-12-28 09:31
zhouyanghao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-12-28
收藏
得分:0 
应该改成字符输出!!
response.setContentType("text/html");
改成
rq.setCharacterEncoding("GB2312");
rp.setContentType("text/html; charset=gb2312");
2007-12-28 10:55
快速回复:关于servlet的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019846 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved