java类向jsp页面传值 java开发的信息系统里,jsp与java文件是怎么传递数据...
js\u6216\u8005jsp\u5411jsp\u4e2d\u7684java\u53d8\u91cf\u4f20\u503c
\u63a5\u6536\u9875\u9762 \u9996\u5148\u5199\u4e2ajava vo \u8981\u4e0e\u8868\u5355\u7684\u5bf9\u8c61\u4e00\u81f4 \u6765\u4fdd\u5b58\u8868\u5355\u63d0\u4ea4\u7684\u503c
\u63a5\u6536\u9875\u9762
//\u7528\u6765\u8c03\u7528vo
\u5f80vo\u4e2d\u5b58\u653e\u503c
//\u53d6\u51faVO\u4e2d\u7684\u503c
\u53d6\u51favo\u503c
\u5176\u4e2d\u53ef\u80fd\u8981\u653e\u5165\u4e00\u4e2a\u5305 \u6211\u8bb0\u4e0d\u5e26\u6e05\u695a\u4e86 \u5305\u540d\u662fjstl.jar
js\u4e5f\u53ef\u4ee5\u5411java\u4f20\u503c \u8fd8\u662f\u8868\u5355\u7684\u5173\u7cfb\u4e0a\u4f20\u7684
\u5728js\u4e2d\u505a\u4e2a\u865a\u62df\u7684\u6807\u70b9\u63d0\u4ea4\u6309\u94ae \u7528js\u7ed9\u8868\u5355\u8d4b\u503c\u540e\u8fdb\u884c\u63d0\u4ea4 \u5c31ok\u4e86
\u3000\u3000jsp\u4e0ejava\u6587\u4ef6\u4f20\u9012\u6570\u636e\u53ef\u4ee5\u4f7f\u7528Servlet\u7c7b\u6765\u4f20\u9012\uff0cjsp\u5c06\u6570\u636e\u5b58\u5165\u5230request\u5bf9\u8c61\u4e2d\uff0cServlet\u7c7b\u83b7\u53d6\u8fd9\u4e2arequest\u5bf9\u8c61\uff0c\u5e76\u5c06\u6570\u636e\u53d6\u51fa\u3002\u3000\u3000\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a\u3000\u3000JSP\u4ee3\u7801\uff1a Demo \u3000\u3000Servlet\u4ee3\u7801\uff1a
public class DemoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name");//\u83b7\u53d6jsp\u9875\u9762\u8f93\u5165\u7684\u53c2\u6570 System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}\u3000\u3000\u8868\u5355\u63d0\u4ea4\u4e4b\u540e\uff0c\u8c03\u7528Servlet\u7c7b\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7request\u5bf9\u8c61\u83b7\u53d6jsp\u9875\u9762\u4f20\u5165\u7684\u53c2\u6570\u503c\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u4f20\u9012\u3002
在test.jsp页面使用<jsp:useBean>标签调用javabean
在test.jsp里使用get()方法就可以得到数据了
//下面是test.jsp
<%@ page contentType="text/html;charset=GB2312"%>
<jsp:useBean id="test" class="test.Testjavajsp" scope="session"/>
<%
out.println("用户名:"+test.getUsername());
%>
//下面是Testjavajsp类
package test;
import java.sql.*;
public class Testjavajsp {
String username="";
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Testjavajsp()
{
String sql="select * from userinfo where userID=1";
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/qquser";
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,"root","123456");
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
username=rs.getString(1);
}
rs.close();
}catch(Exception e){
System.out.println("连接失败");
}
}
}
希望对你有帮助
其实还有很多种方法可以
你将员工的信息封装成一个类,然后将查询后的所有值通过set()方法给对象属性赋值,再将员工对象添加到list里,返回一个list集合,在你的servlet里面用list接,再用req.setAttribute();将list集合传到session中,在jsp页面用c标签遍历输出<c:for Each var="a" items="${list}">
${a.yuangongname} <!--就可以得到员工姓名,其它信息依此输出就是了-->
</c:for Each>
java向jsp传值的方法大致分为以下几类:
1、用<input>输入域获取参数,提交表单
2、request.setAttribute();在下个页面直接request.getAttribute()得到;
3、<jsp:forward="你要跳转到得页面">
<jsp:param>你要传递的参数
4、用seesion传递
5.url传递:url?a=""&b="";
request.setattirbute(rs);
页面,request。getattirbute(rs);
绛旓細//涓嬮潰鏄痶est.jsp <%@ page contentType="text/html;charset=GB2312"%> <jsp:useBean id="test" class="test.Testjavajsp" scope="session"/> < out.println("鐢ㄦ埛鍚嶏細"+test.getUsername());> //涓嬮潰鏄疶estjavajsp绫 package test;import java.sql.*;public class Testjavajsp { String...
绛旓細1.JSP浼犲肩粰Servlet JSP浼犲肩粰Servlet鏈夊嚑绉嶅舰寮忥細Form琛ㄥ崟浼犲硷紝url浼犲硷紝鍏朵粬鏂瑰紡浼犲 a銆乫orm琛ㄥ崟浼犲硷細JSP椤甸潰鏈夛細锛屽皢department鐨刬d浼犲埌Sevlet涓紝鍦ㄧ▼搴忎腑濡備笅锛歋ervletRequest request锛汼tringstaffdepartment=request.getParameter("staffdepartment");鍙幏鍙jsp浼鐨刣epartment锛屼絾瑕佽幏鍙杋d杩樿杩涜杞寲...
绛旓細姣斿鍦ㄥ悗鍙拌缃竴涓煎湪request閲岄潰锛宺equest.setAttribute("param",param);鍦╦sp椤甸潰灏卞彲浠ヨ繖鏍疯幏鍙栵紝${param}; el琛ㄨ揪寮忋傚湪椤甸潰浼犲鍒板悗鍙颁腑锛岀涓绉峱ost鏂瑰紡锛屾彁浜よ〃鍗曟椂銆 鐐瑰嚮鎻愪氦鎸夐挳鍚庡湪鍚庡彴鑾峰彇Username,鍜宲assword String username =request.getParamter("username");String password=reque...
绛旓細鍦java鏂囦欢涓紙閫氬父鏄痵ervlet/struts2锛夌敤request.setAttribute("鍙傛暟鍚"锛屽弬鏁板硷級锛岀劧鍚庡啀jsp椤甸潰涓敤request.getAttribute("鍙傛暟鍚")寰楀埌杩欎釜鍙傛暟.request杩樺彲浠ョ敤session銆乤pplication绛変唬鏇
绛旓細濡傛灉涓嶇敤Servlet鎴栬呰濡係truts杩欑被妗嗘灦鏉ュ仛鍚庡彴澶勭悊鐨勮瘽锛屼綘瀹屽叏鍙互鍦↗SP鏂囦欢閲岀洿鎺ュ啓Java浠g爜鏉ュ鐞嗐<%String pp=request.getParameter("pp"); //琛ㄥ崟鎻愪氦涓婃潵鐨勫弬鏁//鐩存帴璋冪敤smgp鐨凙PI%>
绛旓細1銆佸埄鐢⊿ervletContext杩欎釜web鍏ㄥ眬涓婁笅鏂囨潵鍏变韩鏁版嵁 servlet涓璯etServletContext()鍙互鑾峰緱涓涓猄ervletContext瀵硅薄锛屽埄鐢ㄨ繖涓璞$殑getAttribute()/setAttribute()鏂规硶鍙互鍦ㄦ暣涓猈EB搴旇閲屽叡浜暟鎹紝鍙互瀹炵幇servlet鍜jsp涔嬮棿鐨勬暟鎹簰浼 姣斿锛氬湪servlet涓 getServletContext.setAttribute("title", "hello world"); ...
绛旓細/ 鎻愪氦缁橪oginServlet --- 杩欎釜鏄竴涓猻ervlet鏂囦欢鐨勫悕绉 鐢ㄦ埛鍚嶏細 瀵嗙爜锛 <inpu
绛旓細jsp閫氳繃servlet浼犲煎埌鍙﹀涓涓猨sp鐨勬柟娉曪細jsp閫氳繃form琛ㄥ崟浼犵粰servlet鐨勫奸氳繃request瀵硅薄鍙互浼犲埌鍙﹀涓涓猨sp銆係ervlet浼犲肩粰Jsp 鍏蜂綋瀹炵幇濡備笅锛java浠g爜锛歋tring a= "abccdefg ";request.setAttribute( "ValueA ",a);request.getRequestDispatcher( "缃戝潃/jsp椤甸潰 ").forward(request,response);jsp...
绛旓細"page",page);绗竴涓猵age鏄疭tring绫诲瀷锛屾槸涓洪敭锛岀浜屼釜page鏄硷紝涔熷彲浠ユ槸String绫诲瀷锛屾垨鑰呭叾浠栫被鍨嬨傜劧鍚庡啀鐢 request.getRequestDispatcher("椤甸潰璺緞");杩欐牱杩欎釜page鍊煎氨浼犲埌鎸囧畾鐨勯〉闈簡銆傚湪椤甸潰閲岄潰鐢╡l琛ㄨ揪寮忥紝${page} 灏卞彇鍒板间簡锛屽鏋減age閲屾湁age灞炴э紝鍐${page.age}灏辨槸age鐨勫间簡 ...
绛旓細request.setAttribute("parName",value);session.setAttribute("parName",value);