精华内容
下载资源
问答
  • java登录跳转页面

    2020-12-07 18:54:52
    java登录跳转页面 java文件的编辑方式,任意文本加.java后缀即可,一个java文件只能有一个主类,用public调用,如果要打包成程序包,每个文件前先声明包命,如com.QQH,调试单个文件可以直接java *.java,也可先 ...

    java登录跳转页面

    java文件的编辑方式,任意文本加.java后缀即可,一个java文件只能有一个主类,用public调用,如果要打包成程序包,每个文件前先声明包命,如com.QQH,调试单个文件可以直接java *.java,也可先 javac *.java -encoding UTF-8 生成.class文件后,java *,不需要加class后缀即可自动识别
    主类名必须大写并与文件名一致,调试单个文件需注释包名

    #javac Window.java
    #ls
    Window.java Window.class
    #java Window
    

    每个文件的类里一般有两个public方法,一个是主方法如initWindow,一个public static void main(String[] args) {}用来初始化类并调用方法(初始化类并调用主方法,,如class Window:Window w1=new Window(),w1.initWindow)若主方法与类名相同,可省略w1.initWindow()
    下图是一个简单的窗口实现

    //package com.QQH;
    import javax.swing.JFrame;
    
    
    public class Window extends JFrame{
    
    	public void initWindow(){
    		this.setTitle("test window");
    		this.setSize(600,400);
    		this.setLocation(0,0);
    		this.setVisible(true);
    	}
    
    	public static void main(String[] args){
    		Window w = new Window();
    		w.initWindow();
    	}
    
    }
    

    在这里插入图片描述
    用this.set来设置继承JFrame的Window窗口
    接下来来实现一个略为复杂的界面,编辑Main.java

    //package com.QQH;
    
    import java.awt.event.ActionEvent; //event
    import java.awt.event.ActionListener; //listener
     
    import javax.swing.JButton;
    import javax.swing.JFrame; //frame
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
     
    //import tcp.WindowClient;
     
    public class Main {
     
    	public static void main(String[] args) {
    		LoginWindow LW=new LoginWindow();
    		
    	}
     
    }
    
    class LoginWindow extends JFrame{
    	private String username="root";
    	private String password="root";
    	JFrame window;
    	JTextField user;
    	JTextField pwd;
    	JButton Login;
    	
    	public LoginWindow(){
    		window=new JFrame("Login");
    		window.setLayout(null);
    		window.setSize(600, 600);//设置大小
    		window.setLocationRelativeTo(null);//设置居中
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setLayout(null);
    		window.setResizable(false);//设置窗口不可拉伸改变大小
    
    		JLabel username_label =new JLabel("user:");
    		username_label.setBounds(100,100,100,50);
    		window.add(username_label);
    
    		JLabel password_label =new JLabel("password");
    		password_label.setBounds(100,200,100,50);
    		window.add(password_label);
    
    		user=new JTextField();
    		user.setBounds(150, 100, 300, 50);
    		window.add(user);
    
    		pwd=new JPasswordField();//隐藏密码
    		pwd.setBounds(150, 200, 300, 50);
    		window.add(pwd);
    
    		Login=new JButton("Login in");
    		Login.setBounds(250, 300, 100, 50);
    		window.add(Login);
    		
    		
    		
    		window.setVisible(true);//设置面板可见
    	}
    }
    	
    

    在这里插入图片描述
    可以看到比起上个文本,这个有两个类,主类是Main初始化了继承JFrame的loginWindow,是com.QQH的主页面,先在loginWindow类里对窗口,文本和按钮进行了声明,并private赋值用户名与密码变量,
    LoginWindow调用JFrame新建了一个名为Login的窗口,添加了JLabel,user初始化一个JTextField类,密码需要初始化JPasswordField类,步骤都是先初始化一个类,再往window窗口添加,并设置范围,最后一定要加上显示面板可见的setVisible(true)
    参考以下博客
    https://blog.csdn.net/qs17809259715/article/details/88936619?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160706214819726885871262%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=160706214819726885871262&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-1-88936619.first_rank_v2_pc_rank_v29&utm_term=java%E4%BA%8B%E4%BB%B6%E7%9B%91%E5%90%AC
    可以把下面的直接运行试试
    编辑ActionListenerSelf.java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ActionListenerSelf implements ActionListener{
    	public static void main(String[] args) {
    		ActionListenerSelf als = new ActionListenerSelf();
    		als.init();
    	}
    	
    	public void init(){
    		JFrame f = new JFrame();
    		JPanel p = new JPanel();
    		JButton btn = new JButton("click me");
    		
    		btn.addActionListener(this);
    		
    		p.add(btn);
    		f.add(p);
    		
    		f.setSize(400,300);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("event 1");
    	}
    }
    
    

    编辑ActionListenerInner.java

    //package com.QQH
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ActionListenerInner {
    
    	public static void main(String[] args) {
    		ActionListenerInner ali = new ActionListenerInner();
    		ali.init();
    	}
    	
    	public void init(){
    		JFrame f = new JFrame();
    		JPanel p = new JPanel();
    		JButton btn = new JButton("click me");
    		
    		InterfaceDemoB b = new InterfaceDemoB();
    		
    		btn.addActionListener(b);
    		
    		p.add(btn);
    		f.add(p);
    		
    		f.setSize(400,300);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    	}
    	
    	public class InterfaceDemoB implements ActionListener{
    		public void actionPerformed(ActionEvent e) {
    			System.out.println("event 2");
    		}
    	}
    }
    

    编辑AnonymousInner.java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class AnonymousInner {
    
    	public static void main(String[] args) {
    		AnonymousInner ani = new AnonymousInner();
    		ani.init();
    
    	}
    	public void init(){
    		JFrame f = new JFrame();
    		JPanel p = new JPanel();
    		JButton btn = new JButton("click me");
    		btn.addActionListener(new ActionListener() {
    			
    			public void actionPerformed(ActionEvent e) {
    				System.out.println("AnonymousInner");
    				
    			}
    		});
    		
    		p.add(btn);
    		f.add(p);
    		
    		f.setSize(400,300);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    	}
    }
    

    接下来实现一个引用了外部类的事件监听方式,可以把引用类打包成jar文件放到java的决定包的jre的\lib\ext里方便引用,也可放一个文件夹下
    编辑ExternelclassListener.java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ExternelclassListener {
    
    
    	public static void main(String[] args) {
    		
    		JFrame f = new JFrame();			//创建窗体
    		JPanel p = new JPanel();			//创建面板
    		JButton btn = new JButton("点我");	//创建一个按钮
    		
    		ExternelInterface a = new ExternelInterface(); //初始化外部类当接口事件
    		btn.addActionListener(a);//给bnt添加监听并把新建的事件当触发事件
    		
    		p.add(btn);
    		f.add(p);
    		
    		f.setSize(400,300);					//设置窗体的大小
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗体的后台
    		f.setVisible(true);					//设置窗体可见
    	}
    }
    

    编辑ExternelInterface.java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class ExternelInterface implements ActionListener {
    
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("execternelListener");	
    	}
    }
    

    一般直接直接编译ExternelclassListener.java文件就能直接调用,接口文件与类名正确的话,会把ExternelInterface.java文件一起编译成class文件

    接着实现一个带跳转的页面如调用LoginWindow.java,在接口类加入LoginWindow LW=new LoginWindow();

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class ExternelInterface implements ActionListener {
    
    	public void actionPerformed(ActionEvent e) {
    		LoginWindow LW=new LoginWindow();
    	}
    }
    

    成功跳转
    在这里插入图片描述

    展开全文
  • 1 packagecom.test....23 importjava.io.IOException;4 importjava.io.PrintWriter;5 importjava.util.ArrayList;6 importjava.util.List;78 importjavax.servlet.Filter;9 importjavax.servlet.FilterChain;10 i...

    1 packagecom.test.login;2

    3 importjava.io.IOException;4 importjava.io.PrintWriter;5 importjava.util.ArrayList;6 importjava.util.List;7

    8 importjavax.servlet.Filter;9 importjavax.servlet.FilterChain;10 importjavax.servlet.FilterConfig;11 importjavax.servlet.ServletException;12 importjavax.servlet.ServletRequest;13 importjavax.servlet.ServletResponse;14 importjavax.servlet.http.HttpServletRequest;15 importjavax.servlet.http.HttpServletResponse;16 importjavax.servlet.http.HttpSession;17

    18 public class LoginFilter implementsFilter {19

    20 public static List pattenURL = new ArrayList();21

    22 @Override23 public voiddestroy() {24 //TODO Auto-generated method stub

    25

    26 }27

    28 @Override29 public voiddoFilter(ServletRequest request, ServletResponse response, FilterChain chain)30 throwsIOException, ServletException {31

    32 HttpServletRequest httpRequest =(HttpServletRequest) request;33 HttpServletResponse httpResponse =(HttpServletResponse) response;34 HttpSession session =httpRequest.getSession();35 //登陆url

    36 String loginUrl = httpRequest.getContextPath() + "/login.jsp";37 String url =httpRequest.getRequestURI().toString();38

    39 //注:在pattenURL中的全部不拦截 url.indexOf(urlStr) > -1 表示urlStr在url中出现过,出现就不拦截

    40 for(String urlStr : pattenURL) {41 if (url.indexOf(urlStr) > -1) {42 chain.doFilter(request, response);43 return;44 }45 }46

    47 //超时处理,ajax请求超时设置超时状态,页面请求超时则返回提示并重定向,session.getAttribute("")是获取到登录人的session信息

    48 if (session.getAttribute("") == null) {49 //判断是否为ajax请求

    50 if (httpRequest.getHeader("x-requested-with") != null

    51 && httpRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {52 httpResponse.addHeader("sessionstatus", "timeOut"); //返回超时标识

    53 httpResponse.addHeader("loginPath", loginUrl);//返回url

    54 chain.doFilter(request, response);//不可少,否则请求会出错

    55 } else{56 //alert('会话过期,请重新登录');

    57 String str = "";58 response.setContentType("text/html;charset=UTF-8");//解决中文乱码

    59 try{60 PrintWriter writer =response.getWriter();61 writer.write(str);62 writer.flush();63 writer.close();64 } catch(Exception e) {65 e.printStackTrace();66 }67 }68 } else{69 chain.doFilter(request, response);70 }71 }72

    73 /**

    74 * 过滤器初始化调用方法 在pattenURL中的全部不拦截,所以上面会使用:path.indexOf(urlStr) > -175 */

    76 @Override77 public void init(FilterConfig config) throwsServletException {78 pattenURL.add("login.jsp");//登录jsp

    79 pattenURL.add("login.do");//登录方法

    80 pattenURL.add("css");//css

    81 pattenURL.add("image");//image

    82 pattenURL.add("js");//js

    83 pattenURL.add("fonts");//fonts

    84

    85 }86

    87 }

    展开全文
  • importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importentry.user;importutil.DBUtil;@SuppressWarnings("unused")public classuserDao {//...

    packagedao;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importentry.user;importutil.DBUtil;

    @SuppressWarnings("unused")public classuserDao {//

    public static booleanid(String id,String pwd) {boolean f = false;

    String sql= "select * from user3 where id = '" + id + "' and password = '"+pwd+"'";// Connection conn =DBUtil.getConn();

    Statement state= null;

    ResultSet rs= null;try{

    state=conn.createStatement();

    rs=state.executeQuery(sql);if(rs.next()) {

    f= true;

    }

    }catch(SQLException e) {

    e.printStackTrace();

    }finally{

    DBUtil.close(rs, state, conn);

    }returnf;

    }

    }packageentry;public classuser {privateString id;privateString password;publicString getId() {returnid;

    }public voidsetId(String id) {this.id =id;

    }publicString getPassword() {returnpassword;

    }public voidsetPassword(String password) {this.password =password;

    }publicuser(){}publicuser( String password) {this.password =password;

    };publicuser(String id, String password) {super();this.id =id;this.password =password;

    }

    }packageservlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importdao.userDao;/*** Servlet implementation class AddServlet*/@WebServlet("/AddServlet")public class AddServlet extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/

    protected void service(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

    request.setCharacterEncoding("utf-8");//鑾峰彇鏁版嵁

    String id = request.getParameter("id");

    String password= request.getParameter("password");

    System.out.println(id);

    System.out.println(password);if(userDao.id(id,password)) {

    request.setAttribute("message", "登录成功");

    request.getRequestDispatcher("chenggong.jsp").forward(request,response);

    }else{

    request.setAttribute("message", "登录失败");

    request.getRequestDispatcher("houtai.jsp").forward(request,response);

    }

    }protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

    doGet(request, response);

    }/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub

    response.getWriter().append("Served at: ").append(request.getContextPath());

    }/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/}packageutil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/** 鏁版嵁搴撹繛鎺ュ伐鍏�*/

    public classDBUtil {public static String db_url = "jdbc:mysql://localhost:3306/user?useSSL=false";public static String db_user = "root";public static String db_pass = "root";public staticConnection getConn () {

    Connection conn= null;try{

    Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩

    conn =DriverManager.getConnection(db_url, db_user, db_pass);

    }catch(Exception e) {

    e.printStackTrace();

    }returnconn;

    }/*10鍏抽棴杩炴帴*/

    public static voidclose (Statement state, Connection conn) {if (state != null) {try{

    state.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }if (conn != null) {try{

    conn.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }

    }public static voidclose (ResultSet rs, Statement state, Connection conn) {if (rs != null) {try{

    rs.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }if (state != null) {try{

    state.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }if (conn != null) {try{

    conn.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }

    }public static void main(String[] args) throwsSQLException {

    Connection conn=getConn();

    PreparedStatement pstmt= null;

    ResultSet rs= null;

    String sql="select * from user";

    pstmt=conn.prepareStatement(sql);

    rs=pstmt.executeQuery();if(rs.next()){

    System.out.println("绌�");

    }else{

    System.out.println("涓嶇┖");

    }

    }

    }

    展开全文
  • packagecom.office....importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public cl...

    packagecom.office.DBUtil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public classDBUtil {public static String db_url = "jdbc:mysql://localhost:3306/luizhuan";public static String db_user = "root";public static String db_pass = "wyp6666";public staticConnection getConn () {

    Connection conn= null;try{

    Class.forName("com.mysql.jdbc.Driver");

    conn=DriverManager.getConnection(db_url, db_user, db_pass);

    }catch(Exception e) {

    e.printStackTrace();

    }returnconn;

    }//end getConn

    public static voidclose (Statement state, Connection conn) {if (state != null) {try{

    state.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }if (conn != null) {try{

    conn.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }

    }public static voidclose (ResultSet rs, Statement state, Connection conn) {if (rs != null) {try{

    rs.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }if (state != null) {try{

    state.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }if (conn != null) {try{

    conn.close();

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }

    }public static void main(String[] args) throwsSQLException {

    Connection conn=getConn();

    PreparedStatement pstmt= null;

    ResultSet rs= null;

    String sql="select * from user";

    pstmt=conn.prepareStatement(sql);

    rs=pstmt.executeQuery();if(rs.next()){

    System.out.println("连接成功");

    }else{

    System.out.println("连接失败");

    }

    }

    }

    展开全文
  • } } // * 上述代码,你可以参考下我的方法,我也很久没做JAVA开发了,我现在从事前端UI开发,本来我想在写一个用struts 2登录的程序的,可我现在忘得差不多了,上面我所用到的属性建议你自己好好的研究一下,往后你...
  • 该楼层疑似违规已被系统折叠隐藏此楼查看此楼import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax...
  • io.datafxflow8.0.1public class Tutorial4Main extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {new Flow(Wizard...
  • 本文实例为大家分享了java实现登录案例的具体代码,供大家参考,具体内容如下一、环境搭建JDK1.8 + Tomcat1.8二、目录结构三、代码示例3.1、fail.html页面faill.html亲, 你的用户名或密码输入有误!请重新输入!返回...
  • 首先进入生活号配置生活号后台,目前可在广告位、营销位、自定义菜单、群发消息、模板消息、自动回复等配置中设置跳转小程序。配置跳转小程序前提生活号需要关联小程序,否则不能选择对应的小程序进行配置。1、可在...
  • 第8章 登录功能实现-登录成功跳转主页面8.1涉及的技术知识点1) Session会话 CookieJSTL标签8.2Cookie1) HTTP是无状态协议,服务器不能记录浏览器的访问状态,也就是说服务器不能区分中两次请求是否由一个客户端发出...
  • 初学Java Web页面间的登录跳转
  • springmvc控制登录用户session失效后跳转登录页面,废话不多少了,具体如下:第一步,配置 web.xml15第二步,配置spring-mvc.xml第三步,写拦截器SystemSessionInterceptor 方法public class ...
  • 执行下列代码跳转链接 ClientScript.RegisterStartupScript(Page.GetType(), "", ""); 跳转链接之后发现cookie丢失 有遇到过此情况的大神请指点一二。 线索1cookie可能未成功存入。线索2 未改动代码,换一个浏览器...
  • /** * 跳转登录Servlet */ public class LoginPrompServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ...
  • 使用Servlet实现登录页面并跳转具体内容为前端数据获取以及请求转发。1、新建一个Requeset工程,并且完善工程目录结构。 2、更新WEB-INF文件下的web.xml文件,替换为如下内容。xmlns:xsi=...
  • import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.RequestDispatcher;import javax.ser...
  • importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.sql.*;publicclassUserLoginextendsJFrameimplementsActionListener{DataBaseManagerdb=newDataBaseManage...import java.awt.*;import ja...
  • 首先新建webproject并添加Tomcat Server 网页主体设置 <%-- Created by IntelliJ IDEA. User: 25477 Date: 2020/6/29 Time: 10:25 To change this template use File | ...charset=UTF-8" language="java

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,759
精华内容 703
关键字:

java登录跳转

java 订阅