怎么在servlet设置值,然后怎么在jsp里得到?
我现在用mvc做项目,在servlet如何设置一个值,然后怎么在jsp页面里得到设置的那个值?...
我现在用mvc做项目,在servlet如何设置一个值,然后怎么在jsp页面里得到设置的那个值?
展开
2个回答
展开全部
我个人比较倾向于在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"%>
<jsp:useBean id="article" type="com.zifangsky.OnlineFriend.model.article.Article" scope="request"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<%@ include file="head_article.txt"%>
</head>
<body>
<center><br>
<table border=0 cellspacing=10>
<tr>
<td align=center><font size=5><jsp:getProperty name="article" property="title" /></font></td>
</tr>
<tr>
<td align=left><font size=2><font color=#cea000>来源:</font><font color=#00c73a><jsp:getProperty name="article" property="author" /></font></font></td>
</tr>
<tr>
<td align=left><font size=3><jsp:getProperty name="article" property="content" /></font></td>
</tr>
<tr>
<td align=right>
<form action="ArticleModify" method="get">
<input type="hidden" name="articleId" value=<jsp:getProperty name="article" property="id" />>
<input type="hidden" name="author" value=<jsp:getProperty name="article" property="author" />>
<input type="submit" value="修改">
</form>
</td>
<td align=right>
<form action="ArticleDelete" method="post">
<input type="hidden" name="articleId" value=<jsp:getProperty name="article" property="id" />>
<input type="hidden" name="author" value=<jsp:getProperty name="article" property="author" />>
<input type="submit" value="删除">
</form>
</td>
</tr>
</table>
</center>
</body>
</html>
好了,基本的处理逻辑就是这样了
展开全部
servlet中:
request.setAttribute("userName","1111");
jsp页面中
${userName}或者<%=request.getParameter("userName")%>
request.setAttribute("userName","1111");
jsp页面中
${userName}或者<%=request.getParameter("userName")%>
追问
用session可以不?
追答
可以,用session的话,只要你不取消(session.remove("")),一直会存在的
不过一般的参数是不用session的,除非是整个项目经常性使用的参数。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询