
二、页面的跳转方式
1.通过HTML超链接的方式进行跳转
<a href = "路径">资源地址</a>
2.通过js的location对象进行页面跳转
window.location.href = "路径";
通过超链接跳转到index.jsp页面
通过超链接实现跳转时可以携带参数吗? 可以携带参数,并且可以在目的地通过request对象进行获取该参数-->
注意事项: 第一个参数之前 (?) 参数与参数之间使用(&)
<a href = "index.jsp?username=admin&password=123">跳转到主页</a>
<!-- js跳转 -->
<hr/>
<!-- 通过location跳转可以携带参数 后面可以通过request对象进行获取 -->
<button onclick = "add();">跳转到index.jsp</button>
<script type="text/javascript">
function add(){
/* 通过js的location对象进行页面跳转 */
location.href = "index.jsp?result=123";
}
</script>
中转页面:
作用:
1.获取页面提交的数据
2.进行数据库交互
3.判断是否提交到指定的页面去
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 通过request对象获取数据 脚本区域 -->
<%
//设置编码格式(解决不兼容中文的问题)
request.setCharacterEncoding("utf-8");
//获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//out.println("username="+username+" password = "+password);
//模拟:假设有admin用户,再次注册无效 返回注册页面重登陆 如果不是admin直接注册
if("admin".equals(username)){//我目前注册的账号admin在数据库中已存在 不能注册的
//告诉用户 数据库已存在用户名 不能再次注册 跳回register.jsp页面重写进行注册
//超链接 location跳转 out对象可以拼接标签代码(html js)
out.println("<script>");
out.println("alert('该用户已被注册,请重新进行注册');");
out.println("location.href='register.jsp'");
out.println("</script>");
}else{//可以注册
//直接可以跳转到index.jsp
/* 通过location页面跳转 */
//out.println("<script>");
//out.println("location.href='index.jsp'");
//out.println("</script>");
/* 通过Java的方式 转发进行跳转 */
/*
1.转发 是有服务端发起的跳转
2.转发 跳转页面后,地址栏不会发生改变
3.转发 跳转页面后,上一个页面的数据会保留下来 可以在跳转后的页面上进行获取 通过reuqest对象获取
*/
//response 内置对象 响应
//request.getRequestDispatcher("index.jsp").forward(request, response);
/* 通过Java的方式 重定向进行跳转 */
/*
(1)地址栏:显示新的地址
(2)请求次数:2次
(3)根目录:http://localhost:8080/ 没有项目的名字
(4)请求域中的数据会丢失,因为是2次请求
*/
response.sendRedirect("index.jsp");
}
%>
总结:页面跳转方式 4种 超链接这种跳转方式不适合在java中进行
lcoation和重定向(response.sendRedirect("index.jsp");)是一样
地址栏发生改变,数据不会保留
转发:request.getRequestDispatcher("index.jsp").forward(request, response);
地址栏不会发生改变,数据会保留 可以通过request对象进行获取。
3.通过java方式跳转
3.1 请求转发:
getRequestDispatch(String path).forward(request,response)
3.2 重定向:
sendRedirect(url)
out.print("<script>alert('账号或密码错误');location.href='login.jsp'</script>");
//属于重定向

请求转发和重定向都是页面跳转
1.请求转发:
相当于是张三找李四借钱,但是李四没钱,李四就跑去银行借钱,再把钱借给张三,这里张三值发送了一次请求,但是至少经历了两次转发才拿到钱,但是是从李四手里拿到的钱,所以他的借钱目的人就是李四,向银行借钱这部操作是李四自身进行的,与张三无关
2.重定向:
相当于是张三找李四借钱,但是李四没钱,李四告诉李三可以去银行借钱,张三又跑去银行把钱借了出来,这里张三发送了两次借钱的请求,但是第一次没有借钱成功,第二次才从银行里拿到钱,所以他的借钱人就是银行.
3.转发和重定向的区别
1.请求转发属于内部跳转,服务器内部转发,整个过程处于同一个请求当中,不可以跳到其他网站,只能在同一个网站内跳不同的页面
2.重定向则是站外跳转,浏览器将会得到跳转的地址,并重新发送请求链接。这样,从浏览器的地址栏中可以看到跳转后的链接地址。不在同一个请求。重定向,实际上客户端会向服务器端发送两个请求3…取值
请求转发(站内跳转):可以获取保存在request作用域中的数据
重定向(站外跳转): 不能获取保存在request作用域中的数据
4.地址栏
请求转发(站内跳转):地址栏不发生变化
重定向(站外跳转):地址栏显示跳转页面路径
5.请求次数
请求转发(站内跳转):仅发起一次请求,所以可以获取到保存在request中的数据
重定向(站外跳转):发起多次请求,所以不能获取request中的数据
6.性能(效率)
(站外跳转): 不能获取保存在request作用域中的数据
4.地址栏
请求转发(站内跳转):地址栏不发生变化(
重定向(站外跳转):地址栏显示跳转页面路径
5.请求次数
请求转发(站内跳转):仅发起一次请求,所以可以获取到保存在request中的数据
重定向(站外跳转):发起多次请求,所以不能获取request中的数据
6.性能(效率)
请求转发(站内跳转) > 重定向(站外跳转)
--创建用户表
create table tb_users
(
user_id number primary key,--用户编号
username varchar2(100),--用户名
password varchar2(100)--密码
)
--插入默认的数据
insert into tb_users values(1,'zkingzz','123');
insert into tb_users values(2,'admin','123');
commit;--事务提交 数据永久保存下来,web程序可以访问得到。
select * from tb_users;
--如果web应用程序与Oracle进行数据交互,那么在Oracle中手动添加的数据或者修改的数据或者删除的数据
--必须做处理???提交 commit
--如果不提交事物,意味着web应用程序中访问不到这些手动添加,修改,删除的数据
2. 将指定的Oracle的jar包导入项目(jar包根据自己的需求可在浏览器进行下载)
3.在JavaWeb中的jsp文件中步骤如下:
1.加载驱动Class.forName
2.建立连接 Connection
3.创建执行对象 PreparedStatement
4.影响的行数或者结果集 ResultSet
5.关闭连接 close();
1~5:
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
/* 作用:登录页面提交后的处理数据的页面 */
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//3.进行Oracle数据库交互
//定义两个变量存储用户名以及密码
String uname = "";
String pwd = "";
//连接对象定义
Connection conn = null;
PreparedStatement ps = null;//执行对象
//结果集对象
ResultSet rs = null;
try{
//a.加载驱动
//快捷方式 倒入驱动路径 OracleDriver
Class.forName("oracle.jdbc.driver.OracleDriver");
//b.建立连接
String url = "jdbc:oracle:thin:@localhost:1521:orcl";//强调一下 每个人的url值的最后不一样
//但是大部分人是一样 服务窗口中查看OracleServiceORCL 这个服务 ORCL后面有没有字母
conn = DriverManager.getConnection(url, "scott", "123");
//c.编写sql语句传入执行方法返回执行对象
String sql = "select * from tb_users where username = ? and password = ?";
ps = conn.prepareStatement(sql);
//d.占位符赋值
ps.setString(1, username);
ps.setString(2, password);
//e.返回结果集对象
rs = ps.executeQuery();
//d.遍历或者判断
if(rs.next()){//如果结果集中存在下一条数据
//有用户返回出来了 用变量接收
uname = rs.getString(2);
pwd = rs.getString(3);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null&&!conn.isClosed()){
conn.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
}
//4.页面跳转
out.println(uname+" "+pwd);
//数据交互后,如何判断进行页面你跳转
//只要判断uname和pwd中是否存在值 如果存在 说明登录成功 跳转到主页面
//如果没有值 返回登录页面继续登录即可。
//"" 与 null 是有区别的。
if(uname!="" && pwd!=""){
//System.out.println("ok");
//使用转发的技术跳转到主页面 主页面显示当前登录的账号
request.getRequestDispatcher("home.jsp").forward(request, response);
}else{
//System.out.println("no ok");
out.println("<script>alert('账号或密码错误');location.href='login.jsp';</script>");
}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<h2>这是新闻发布系统的主页面</h2>
<b>欢迎<%=request.getParameter("username") %> 回家</b>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<h3>用户登录</h3>
<hr/>
<form action = "doLogin.jsp" method = "post">
账号 <input type = "text" name = "username"/>
<br/><br/>
密码 <input type = "password" name = "password"/>
<br/><br/>
<input type = "submit" value = "登录"/>
</form>
</body>
</html>
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳