java怎么普通类中request的使用以及jsp页面取值,求代码
5个回答
2015-11-25 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
步骤如下:
1、在web工程里面创建一个Servlet类,继承HttpServlet,重写doPost,doGet方法,在doPost方法中调用doGet方法;
2、在doGet方法中把要设置到jsp页面的值存到request中;
3、在doGet方法中添加转发到jsp页面的代码;
4、在jsp页面中使用jstl标签获取存入的值。
事例代码如下:
Servlet类:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("name", "nameValue");
request.getRequestDispatcher("/demo.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Demo</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
${name }
</body>
</html>
其中,<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>表示导入jstl标签库,没导入的话无法使用jstl标签,使用jstl标签可以减少很多代码量,导入jstl标签后就可以通过使用${}的方法来获取值了。
1、在web工程里面创建一个Servlet类,继承HttpServlet,重写doPost,doGet方法,在doPost方法中调用doGet方法;
2、在doGet方法中把要设置到jsp页面的值存到request中;
3、在doGet方法中添加转发到jsp页面的代码;
4、在jsp页面中使用jstl标签获取存入的值。
事例代码如下:
Servlet类:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("name", "nameValue");
request.getRequestDispatcher("/demo.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Demo</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
${name }
</body>
</html>
其中,<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>表示导入jstl标签库,没导入的话无法使用jstl标签,使用jstl标签可以减少很多代码量,导入jstl标签后就可以通过使用${}的方法来获取值了。
展开全部
public class LoginServlet extends HttpServlet { //继承HttpServlet
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response); //合并doGet和doPost
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username"); //获取页面表单的值
String password = request.getParameter("password");
User u = new User();
u.setUsername(username);
u.setPassword(password);
CheckDao cd = new ICheckDao();
boolean flag = cd.checklogin(u); //到数据库匹配
if (flag) {
request.getSession().setAttribute("username", username); //返回用户名
}
}
}
JSP页面:
<c:choose>
<c:when test="${empty username}">
<li class="pic btn txt"><a href="login.jsp" target="_parent">登录</a></li>
<li class="pic btn txt"><a href="register.jsp" target="_parent">注册</a></li>
</c:when>
<c:otherwise>
<li class="pic btn txt"><a href="#">${username}</a></li>
</c:otherwise>
</c:choose>
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response); //合并doGet和doPost
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username"); //获取页面表单的值
String password = request.getParameter("password");
User u = new User();
u.setUsername(username);
u.setPassword(password);
CheckDao cd = new ICheckDao();
boolean flag = cd.checklogin(u); //到数据库匹配
if (flag) {
request.getSession().setAttribute("username", username); //返回用户名
}
}
}
JSP页面:
<c:choose>
<c:when test="${empty username}">
<li class="pic btn txt"><a href="login.jsp" target="_parent">登录</a></li>
<li class="pic btn txt"><a href="register.jsp" target="_parent">注册</a></li>
</c:when>
<c:otherwise>
<li class="pic btn txt"><a href="#">${username}</a></li>
</c:otherwise>
</c:choose>
追问
那怎么在一个普通的类(非serverlet)里往前台传值呢?
追答
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/xml;charset=UTF-8");
//设置响应对象中的缓存是否启用
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.print(msg); //msg就是传递到前台的数据 msg="{name:'zhangsan'}"
out.close();
JSP:
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
request在普通的类中不能使用。必须在Servlet 或者 过滤器 的类中使用,
页面上取值:request.getAttribute("param");
页面上取值:request.getAttribute("param");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
request.setattribute("value",value);
${requestSCOPE.value}
${requestSCOPE.value}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询