java怎么返回到jsp页面数据 jsp中如何把数据库中查询出来的数据输出到jsp页面。
\u600e\u4e48\u5728jsp\u9875\u9762\u83b7\u53d6java\u5728\u540e\u53f0\u4ea7\u751f\u7684\u53d8\u91cf\u503c\uff0c\u4ee5\u53ca\u4ece\u9875\u9762\u4f20\u8f93\u5c45\u5230java\u7a0b\u5e8f\uff08\u7a0b\u5e8f\u4e0d\u662f\u5d4c\u5728\u9875\u9762\u7684\uff09\u6bd4\u5982\u5728\u540e\u53f0\u8bbe\u7f6e\u4e00\u4e2a\u503c\u5728request\u91cc\u9762\uff0crequest.setAttribute("param",param);
\u5728jsp\u9875\u9762\u5c31\u53ef\u4ee5\u8fd9\u6837\u83b7\u53d6\uff0c${param}; el\u8868\u8fbe\u5f0f\u3002
\u5728\u9875\u9762\u4f20\u503c\u5230\u540e\u53f0\u4e2d\uff0c\u7b2c\u4e00\u79cdpost\u65b9\u5f0f\uff0c\u63d0\u4ea4\u8868\u5355\u65f6\u3002
\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\u540e\u5728\u540e\u53f0\u83b7\u53d6Username,\u548cpassword
String username =request.getParamter("username");
String password=request.getParamter("password");
\u5982\u679c\u662fget\u65b9\u5f0f\u63d0\u4ea4\u7684\u8bdd
\u4e3e\u4e2a\u4f8b\u5b50\uff1a
\u67e5\u770b
\u90a3\u4e48\u5728\u540e\u5728\u83b7\u53d6\u4e5f\u662f\u4e00\u6837\u7684
String param1=request.getParamter("param1");
1\u3001\u628a\u6570\u636e\u5e93\u5efa\u597d
CREATE TABLE `User` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
2\u3001\u63d2\u5165\u4e00\u4e2a\u6570\u636e
insert into User values(1,"Martin","12345","zjut")
3\u3001\u5efa\u7acb\u4e00\u4e2aDynamic Web Project\uff0c\u76ee\u5f55\u5982\u4e0b\uff1a
4\u3001showinfo.jsp\uff0c\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\u5bfc\u5165\u7c7b\uff1aimport="com.entity.User"
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.entity.User"%>
Insert title here
<%
User user = (User)request.getAttribute("user");
%>
\uff08\u6b64\u5904\u7a7a\u4e00\u884c\uff09
id:
username:
password:
address:
\uff08\u6b64\u5904\u7a7a\u4e00\u884c\uff09
<!--
\u4e5f\u53ef\u4ee5\u7528el\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f\u540c\u6837\u9700\u8981servlet\u5148\u5c06\u503c\u5b58\u653e\u5728request\u4e2d\uff0c\u4f46\u662f\u7528el\u8868\u8fbe\u5f0f\u7684\u8bdd\u9700\u8981\u5728\u4e0a\u9762\u52a0\u4e0a\uff1a
\u5982\u4e0b\uff0c\u83b7\u53d6id\u8fd9\u6837\u5199\u5c31\u53ef\u4ee5
${user.getId()}
-->
5\u3001\u7ed3\u679c\u5982\u4e0b\uff1a
然后再用
request.getRequestDispatcher("页面路径");
这样这个page值就传到指定的页面了。
在页面里面用el表达式,${page} 就取到值了,如果page里有age属性,再${page.age}就是age的值了
通过ajax传json对象到后台,也通过ajax接收咯
绛旓細3. 鏁版嵁娓叉煋鍒板墠绔〉闈 鏈鍚庝竴姝ユ槸灏嗗皝瑁呭ソ鐨勬暟鎹覆鏌撳埌鍓嶇椤甸潰銆傚湪Java Web寮鍙戜腑锛屾湁澶氱鎶鏈彲浠ュ疄鐜拌繖涓鐐癸紝鍖呮嫭JSP锛圝avaServer Pages锛夈乀hymeleaf鍜孎reeMarker绛夋ā鏉垮紩鎿庛傝繖浜涙妧鏈厑璁镐綘鍦℉TML椤甸潰涓祵鍏ュ姩鎬佸唴瀹癸紝杩欎簺鍐呭鍦ㄦ湇鍔″櫒绔鐞嗗苟濉厖鏁版嵁銆備緥濡傦紝鍦↗SP椤甸潰涓紝浣犲彲浠ヤ娇鐢‥L锛圗xpression ...
绛旓細protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { //姝ゅ娣诲姞鐢ㄦ埛瀵嗙爜淇℃伅鍒ゆ柇浠g爜锛屼箣鍚庤蛋涓嬮潰鐨勬柟娉曞嵆鍙皟杞埌鎯宠鐨鐣岄潰 this.getServletConfig().getServletContext().getRequestDispatcher("/CFRT02.jsp").forward(request, response);}...
绛旓細姝ラ濡備笅锛1銆佹墦寮瑕佹煡鐪嬬殑JSP椤甸潰銆2銆佸彸閿鍦椤甸潰涓婄偣鍑伙紝閫夋嫨鈥淥pen With鈥濄3銆佺劧鍚庨夋嫨鈥淲eb page edit鈥濆嵆鍙煡鐪嬮〉闈㈠浘褰
绛旓細2銆佸鏋滈潪瑕佸啓鍦╦sp椤甸潰鐨勮瘽锛屽缓璁敤EL琛ㄨ揪寮+jstl鏍囩搴撹繘琛屽惊鐜(PS:鍗冧竾鍒棶鎴戜粈涔堟槸EL琛ㄨ揪寮忓拰JSTL)銆3銆佸叧浜庡彧鏈変竴鏉℃暟鎹殑闂銆傛柇鐐硅皟璇曟垨鑰呰緭鍑鸿鍙ワ紝鐪嬬湅闆嗗悎鐨剆ize();鍒板簳鏈夊嚑鏉℃暟鎹 濡傛灉杩樻槸鍙湁涓鏉℃暟鎹紝骞朵笖鑷繁鐢ㄧ殑oracle鏁版嵁搴 妫鏌ュ湪鏁版嵁搴撴彃鍏ユ暟鎹殑鏃跺 鍏朵粬鏁版嵁鏄惁鐢╟ommit;鎻愪氦銆
绛旓細涓句釜渚嬪瓙 public class H{ String str; public void setStr(String str){ this.str=str; }}鍦╦sp椤甸潰 <%String str=request.getParameter("user"); //鑾峰彇琛ㄥ崟鐨勫糎.setStr(str); //杩欐牱灏辨妸鍊浼犲埌java绫讳簡%>宸笉澶氬氨鏄繖涓祦绋 ...
绛旓細鍔炴硶濡備笅锛1銆佸皢Java闆嗗悎浼犻缁橨SP椤甸潰锛氬湪Servlet鎴栨帶鍒跺櫒涓紝灏嗛渶瑕佽緭鍑虹殑Java闆嗗悎瀛樺偍鍦ㄨ姹傛垨浼氳瘽灞炴т腑銆2銆鍦↗SP椤甸潰涓娇鐢↗STL锛氬鍏STL鐩稿叧鐨勬爣绛惧簱锛屼互渚垮湪JSP椤甸潰涓娇鐢ㄣ3銆佷娇鐢↗STL鐨勫惊鐜爣绛鹃亶鍘嗛泦鍚堬細浣跨敤鏍囩閬嶅巻闆嗗悎锛屽皢闆嗗悎涓殑姣忎釜鍏冪礌杩涜杩唬澶勭悊锛屽苟鍦ㄥ惊鐜腑杈撳嚭鐩稿簲鐨勬暟鎹
绛旓細request.setAttribute("test", test);椤甸潰鐩存帴鍙${test} 灏辨垚浜
绛旓細鍦浣犵殑jsp鏂囦欢绗竴琛宲age鏍囩閲屽姞涓奿sELIgnored="false"
绛旓細浣犳槸鎯冲仛鍒癑SP椤甸潰涓嶅姞鍏<%%>杩欑灏忚剼鏈惂,閭e彲浠ラ噰鐢‥L琛ㄨ揪寮忥紝鎴栬呬竴浜涙鏋剁殑杈撳嚭鏂瑰紡锛岄氬父鐢‥L灏卞彲浠ヤ簡锛屽洜涓篍L灏辨槸鐢ㄦ潵杩愮畻鍜岃〃杈剧殑銆傚悗鍙拌幏鍙栨暟鎹互鍚庯紝杩欎釜鏁版嵁鍙互鏄竴涓畝鍗曠被鍨嬶紝涔熷彲浠ユ槸涓涓璞★紝濡傛灉鏄悓涓涓姹備笅鐨勶紝涔熷氨鏄痳equest瀵硅薄娌℃湁鍙橈紝閭d箞鍙互鐢ㄨ繖涓瓨鍌紝鍚﹀垯瑕佺敤session瀛樺偍锛...
绛旓細1.浣鍦浣犵殑绫讳腑澹版槑涓涓鏈夊彉閲忊減rivate String path鈥濓紝鐒跺悗缁杩欎釜path鐩稿簲鐨刧et鍜宻et鐨刯avabean鏂规硶.2.鍦╯truts2鐨剎ml閰嶇疆鏂囦欢涓厤缃柟娉 3.鐒跺悗鍦ㄤ綘鐨jsp椤甸潰涓煇涓猟iv涓樉绀哄氨鐢╯truts2鐨勬爣绛 杩欐牱灏監K浜嗐傘傘傛湁鍏硈trut2鍏蜂綋鐨勭敤娉曚綘濡傛灉涓嶇啛鎮夌殑璇濊繕鏄湅鐪媠truts2鐨勭浉鍏宠祫鏂欏惂銆