我个人比较倾向于在servlet里把数据存在javabean里,然后jsp显示页面通过调用相应的javabean就可以取出在servlet里的值,这样的一个好处是可以把后台的处理逻辑和数据存储分开,给你一个例子看看就明白了
下面的例子在新闻管理中,通过点击新闻的标题超链接,然后从数据库中取出相对应的新闻正文,再把新闻的相关信息(新闻标题,作者,正文)放入javabean(PS:这里是Article.java)中,最后在JSP页面中显示
Article.java :
package com.zifangsky.OnlineFriend.model.article;
public class Article {
private int id; //文章id
private String title,author,content; //新闻标题,作者,正文
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
处理逻辑(servlet)(PS:这里是:HandleArticleShowContent.java)
HandleArticleShowContent.java :
package com.zifangsky.OnlineFriend.servlet.article;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zifangsky.OnlineFriend.model.article.Article;
import com.zifangsky.OnlineFriend.util.DbConn;
public class HandleArticleShowContent extends HttpServlet{
public void init(ServletConfig config)throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
//查看文章不需要登录
continueDoGet(request,response);
}
private void continueDoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String articleId = request.getParameter("id");
Article article = new Article();
request.setAttribute("article", article);
Connection connection = DbConn.getConnection();
try {
PreparedStatement pStatement = connection.prepareStatement("select id,title,author,content from article where id=?");
pStatement.setString(1, articleId);
ResultSet resultSet = pStatement.executeQuery();
if(resultSet.next()){
article.setId(resultSet.getInt(1));
article.setTitle(resultSet.getString(2));
article.setAuthor(resultSet.getString(3));
article.setContent(resultSet.getString(4));
}
pStatement.close();
connection.close();
} catch (Exception e) {
}
RequestDispatcher dispatcher = request.getRequestDispatcher("article/showArticle.jsp");
dispatcher.forward(request, response);
}
}
最后是页面显示(PS:这里是:showArticle.jsp)
showArticle.jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.zifangsky.OnlineFriend.model.article.Article"%>
">
<%@ include file="head_article.txt"%>
来源:
好了,基本的处理逻辑就是这样了
servlet中:
request.setAttribute("userName","1111");
jsp页面中
${userName}或者<%=request.getParameter("userName")%>