java頁面自動跳轉(zhuǎn)代碼 java如何實(shí)現(xiàn)跳轉(zhuǎn)到指定頁面
用java怎樣編寫登錄頁面,成功登錄跳轉(zhuǎn)到下一個頁面,求代碼
說說servlet里面的方法:
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供昭通企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計制作、做網(wǎng)站、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為昭通眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
public void?ValidateUserPass(String user,String pass){
RequestDispathcher?rd =null
//假使你的代碼是從DB中獲取
DBFactory?db=DBFactoryImpl.getDBFactoryInstance();//得到數(shù)據(jù)庫鏈接
flg=db.findUser(user,pass);
//?這里是不存在用戶
if(flg.hasNext()==-1){
//?登錄時錯誤了,一般我們會給用戶一個提示
session.setAttirbute("msg","對不起,用戶名或密碼錯誤");
RequestDispathcher?rd?=?req.getRequesDispatcher("login.jsp");
rd.forward(request,?reponse);//將請求對象和響應(yīng)對象傳遞進(jìn)來
}???//?這里是存在當(dāng)前用戶
else{
//當(dāng)然這里登錄成功時,我們要把當(dāng)前用戶寫到session里面保存
session.setAttirbute("userName",user);
//這個請求轉(zhuǎn)發(fā)語句
request.sendRedirect("index.html");
}
}
// * 上述代碼,你可以參考下我的方法,我也很久沒做JAVA開發(fā)了,我現(xiàn)在從事前端UI開發(fā),本來我想在寫一個用struts 2登錄的程序的,可我現(xiàn)在忘得差不多了,上面我所用到的屬性建議你自己好好的研究一下,往后你將學(xué)到struts2 hibernate,Spring等一系列優(yōu)秀的開源框架,說白了,這些東西的底層還是這些,只不過這些框架做了一些封裝隔離。上述代碼建議你重點(diǎn)理解一下:請求轉(zhuǎn)發(fā)和重定向的區(qū)別。
用java做好的登陸界面,當(dāng)?shù)顷懗晒筇D(zhuǎn)到下個頁面的代碼是什么?
用java做好的登陸界面,當(dāng)?shù)顷懗晒筇D(zhuǎn)到下個頁面的代碼如下:
如果登陸驗(yàn)證是在jsp中,那么跳轉(zhuǎn)可以寫成
1.response.sendRedirct("跳轉(zhuǎn)到頁面");
2.jsp:forward page="跳轉(zhuǎn)頁面"/
3.response.setHeader("Location","");
如果是登陸驗(yàn)證是在servlet中,那么中轉(zhuǎn)可以寫成
1.response.sendRedirect("/a.jsp");
2.RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
也可以使用js代碼實(shí)現(xiàn):
script
function validate(){
window.location.href="/index.jsp";
}
/script
java中如何做到界面的跳轉(zhuǎn)?
假如有兩個frame,分別為frame1,frame2,frame1加個按鈕實(shí)現(xiàn)跳轉(zhuǎn).frame1代碼如下
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class frame1 extends JFrame implements ActionListener{
/**
* @param args
*/
private JButton jb;
public frame1()
{
this.setSize(300, 200);
this.setLocation(300, 400);
jb=new JButton("跳轉(zhuǎn)");
this.add(jb);
jb.addActionListener(this);//加入事件監(jiān)聽
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
frame1 frame=new frame1();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb)
{
this.dispose();//點(diǎn)擊按鈕時frame1銷毀,new一個frame2
new frame2();
}
}
}
frame2是個單純的界面
import javax.swing.JButton;
import javax.swing.JFrame;
public class frame2 extends JFrame{
/**
* @param args
*/
public frame2()
{
this.setSize(300, 200);
this.setLocation(300, 400);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
frame2 frame=new frame2();
}
}
Java單擊確定按鈕跳轉(zhuǎn)到另一個界面的代碼。調(diào)到另一個類的界面
public
void
actionPerformed(ActionEvent
e)
{
if(e.getSource()
==
button)
//或者e.getActionCommand().equals("確定')
{
Login
window
=
new
Login();
window.frame.setVisible(true);
}
}
這樣就可以了。但是要在Login類中定義一個全局變量frame,即:private
JFrame
frame,并且記得初始化,frame
=new
JFrame();
分享標(biāo)題:java頁面自動跳轉(zhuǎn)代碼 java如何實(shí)現(xiàn)跳轉(zhuǎn)到指定頁面
分享地址:http://www.xueling.net.cn/article/dohgigg.html