jsp嵌入java代码访问数据库出错

lyz998 学生  2017-12-02 01:16:40
问下,同样的代码,我放到java类里面就可以执行,放到jsp里就报错

我调试了一下,index.jsp总是在加载数据库的地方报空指针异常,test类是我创建的测试类(访问成功)

java test代码
`public static void main(String args[]){
NewsServiceImpl newsService = new NewsServiceImpl();
NewsDao newsDao = new NewsDaoImp();
newsService.setNewsDao(newsDao);

List<News> newsList = new ArrayList<News>();
newsList = newsService.getNewsList();
for (News _new : newsList){
System.out.println("id: "+_new.getId()+"\t title:"+_new.getTitle() +"\t summary:"+_new.getSummary()
+"\t content:"+_new.getContent()+"\t time:"+_new.getCreateDate()+"\t categoryName:"+_new.getCategoryName()+"\t categotyID:"+_new.getCategoryID());
}


``

jsp代码

```
<%@ page import="com.lyz.news.service.NewsService" %>
<%@ page import="com.lyz.news.service.impl.NewsServiceImpl" %>
<%@ page import="com.lyz.news.dao.NewsDao" %>
<%@ page import="com.lyz.news.impl.NewsDaoImp" %>
<%@ page import="com.lyz.news.dao.CategoryDao" %>
<%@ page import="com.lyz.news.impl.CategoryDaoImp" %>
<%@ page import="java.util.List" %>
<%@ page import="com.lyz.news.entity.News" %>
<%@ page import="java.util.ArrayList" %>
<%--
Created by IntelliJ IDEA.
User: LYZ
Date: 2017/11/30
Time: 0:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<table border="1px">
<thead>新闻列表</thead>
<tbody>

<%
NewsServiceImpl newsService = new NewsServiceImpl();
NewsDao newsDao = new NewsDaoImp();
newsService.setNewsDao(newsDao);

List<News> newsList = newsService.getNewsList();

for (News news : newsList){
%>
<tr>
<td><a href=""><%=news.getTitle()%></a></td>
<td><%=news.getAuthor()%></td>
<td><%=news.getCreateDate()%></td>
<td><a href="">删除</a>
<a href=""></a>修改</td>
</tr>
<%
}
%>

</tbody>
</table>
</body>
</html>

```

错误


```Connected to server
[2017-12-02 12:18:46,448] Artifact news:war exploded: Artifact is being deployed, please wait...
[2017-12-02 12:18:46,852] Artifact news:war exploded: Artifact is deployed successfully
[2017-12-02 12:18:46,852] Artifact news:war exploded: Deploy took 404 milliseconds
加载数据库引擎失败
02-Dec-2017 00:18:47.669 信息 [Thread-6] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-1314"]
02-Dec-2017 00:18:48.194 信息 [Thread-6] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
02-Dec-2017 00:18:48.701 信息 [Thread-6] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
02-Dec-2017 00:18:48.703 信息 [localhost-startStop-1] org.apache.catalina.core.StandardWrapper.unload Waiting for [1] instance(s) to be deallocated for Servlet [jsp]
02-Dec-2017 00:18:49.708 信息 [localhost-startStop-1] org.apache.catalina.core.StandardWrapper.unload Waiting for [1] instance(s) to be deallocated for Servlet [jsp]
02-Dec-2017 00:18:50.713 信息 [localhost-startStop-1] org.apache.catalina.core.StandardWrapper.unload Waiting for [1] instance(s) to be deallocated for Servlet [jsp]
02-Dec-2017 00:18:50.836 警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. Stack trace of request processing thread:[
java.lang.Object.wait(Native Method)
java.lang.Thread.join(Thread.java:1245)
java.lang.Thread.join(Thread.java:1319)
java.lang.ApplicationShutdownHooks.runHooks(ApplicationShutdownHooks.java:106)
java.lang.ApplicationShutdownHooks$1.run(ApplicationShutdownHooks.java:46)
java.lang.Shutdown.runHooks(Shutdown.java:123)
java.lang.Shutdown.sequence(Shutdown.java:167)
java.lang.Shutdown.exit(Shutdown.java:212)
java.lang.Runtime.exit(Runtime.java:109)
java.lang.System.exit(System.java:971)
com.lyz.news.dao.BaseDao.getConnection(BaseDao.java:29)
com.lyz.news.impl.NewsDaoImp.getNewsList(NewsDaoImp.java:21)
com.lyz.news.service.impl.NewsServiceImpl.getNewsList(NewsServiceImpl.java:36)
org.apache.jsp.index_jsp._jspService(index_jsp.java:151)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)]
02-Dec-2017 00:18:50.845 信息 [Thread-6] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-1314"]
Disconnected from server

运行tomcat 然后就报错退出


```
...全文
273 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lyz998 2017-12-04
我lib目录放错了,应该放到web-inf下
  • 打赏
  • 举报
回复
孟子E章 2017-12-03
一般情况下是有些配置没加载导致,这要进行调试或者看配置解决。你使用的是什么框架? 另外一点,jsp常用来显示最终数据的,数据库的查询是在java里面做的,jsp里面只是对数据的结果进行显示。
  • 打赏
  • 举报
回复
相关推荐
发帖
其他
加入

1.0w+

社区成员

Web 开发 其他
申请成为版主
帖子事件
创建了帖子
2017-12-02 01:16
社区公告
暂无公告