在jsp中如何用request中获取后台传来的数据

在jsp中如何用request中获取后台传来的数据,第1张

用requestgetAttribute获取,而不是getParameter。

>

当两个Web组件之间为链接关系时,被链接的组件通过getParameter方法来获得请求参数

String datetime1 = (String)requestgetAttribute("datetime");

通常情况下,你每次提交(点击搜索)的时候,

过程如下: 转到后台,根据你的输入生成新的sql语句转到后台

数据库查询出新的表格内容

转到前台展示整个页面

到了展示的这一步,会生成新的页面,虽然是跟上一个一样的页面,其实他的内容都是重新生成显示的

最好的办法先把它给一个表单里,再调用,

<form name="f">

<textarea name=t style="display:none"><%=results%></textarea>

</form>

<script>

function winowonload()

{

alert(focumentftvalue)

}

</script>

jsp在页面上获取java参数总共有以下方法:

(1)直接在URL请求后添加

如:<a href="thexuanjspaction=transparams&detail=directe")直接传递参数, 特别的在使用responsesendRedirect做页面转向的时候,也可以用如下代码: responsesendRedirect("thexuanjspaction=transparams&detail=directe") ,可用requestgetParameter(name)取得参数

(2)jsp:param

它可以实现主页面向包含页面传递参数,如下:

<jsp:include page="Relative URL">

<jsp:param name="param name" value="paramvalue" />

</jsp:include>

还可以实现在使用jsp:forward动作做页面跳转时传递参数,如下:

<jsp:forward page="Relative URL">

<jsp:param name="paramname" value="paramvalue" />

</jsp:forward> 通过这种方式和一般的表单参数一样的,也可以通过requestgetParameter(name)取得参数

(3)设置session和request

通过显示的把参数放置到session和request中,以达到传递参数的目的

sessionsetAttribute(name,value);

requestsetAttribute(name,value)

取参数:value=(value className)sessiongetAttribute(name);

value=(value className)requestgetAttribute(name);

jsp的数据传给servlet的方法:

第一种超链接,在href='servletActionid=xxx'

第二种form表单提交,当点击submit时,action="servletAction" method='get/post'

第三种就是利用ajax,url='servletActionz',当然了,这里也有get和post两种情况,get直接在url后面加问号传参,post会把参数放在实体内容里面。

最后在servlet那边 用 requestgetParameter("参数名");就可以拿到了

最简单的JSP页面中的数据库 *** 作方法:

<%@ page

language="java"

contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"

%>

<%@page import="javasql"%>

<center>

<H1> <font color="blue" size="12">管理中心</font></H1>

<HR />

<table width="80%" border="1">

<tr>

<th>ID</th>

<th>书名</th>

<th>作者</th>

<th>价格</th>

<th>删除</th>

</tr>

<%

// 数据库的名字

String dbName = "zap";

// 登录数据库的用户名

String username = "sa";

// 登录数据库的密码

String password = "123";

// 数据库的IP地址,本机可以用 localhost 或者 127001

String host = "127001";

// 数据库的端口,一般不会修改,默认为1433

int port = 1433;

String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username

+ ";password=" + password;

//

//声明需要使用的资源

// 数据库连接,记得用完了一定要关闭

Connection con = null;

// Statement 记得用完了一定要关闭

Statement stmt = null;

// 结果集,记得用完了一定要关闭

ResultSet rs = null;

try {

// 注册驱动

ClassforName("commicrosoftsqlserverjdbcSQLServerDriver");

// 获得一个数据库连接

con = DriverManagergetConnection(connectionUrl);

String SQL = "SELECT from note";

// 创建查询

stmt = concreateStatement();

// 执行查询,拿到结果集

rs = stmtexecuteQuery(SQL);

while (rsnext()) {

%>

<tr>

<td>

<%=rsgetInt(1)%>

</td>

<td>

<a href="prepareupdateID=<%=rsgetInt("ID")%>" target="_blank"><%=rsgetString(2)%></a>

</td>

<td>

<%=rsgetString(3)%>

</td>

<td>

<%=rsgetString(4)%>

</td>

<td>

<a href="deleteID=<%=rsgetInt("ID")%>" target="_blank">删除</a>

</td>

</tr>

<%

}

} catch (Exception e) {

// 捕获并显示异常

eprintStackTrace();

} finally {

// 关闭我们使用过的资源

if (rs != null)

try {

rsclose();

} catch (Exception e) {}

if (stmt != null)

try {

stmtclose();

} catch (Exception e) {}

if (con != null)

try {

conclose();

} catch (Exception e) {}

}

%>

</table>

<a href="insertjsp">添加新纪录</a>

</center>

思路:在JSP页面中使用request内置对象,requestgetServletContext()getContextPath()获取路径。

代码如下:

以上就是关于在jsp中如何用request中获取后台传来的数据全部的内容,包括:在jsp中如何用request中获取后台传来的数据、javascript 怎么得到jsp的数据、java怎么获取jsp页面的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9722083.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存