导航:首页 > 编程语言 > java网页登陆

java网页登陆

发布时间:2023-07-16 09:10:05

‘壹’ java web网页登录功能原理(最好有代码❳

想要实现一个简单的登录功能的话,可以使用Servlet+jsp来实现,jsp编写登录界面和登录后的要出现信息界面和登录失败的信息界面,Servlet类用来对表单提交的用户名和密码进行判断和处理。

具体代码如下:
Servlet类:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String loginname = request.getParameter("loginname");
String password = request.getParameter("password");
if(loginname.equals("a") && password.equals("a")){
request.setAttribute("msg", "登录成功");
request.getRequestDispatcher("/loginsuccess.jsp").forward(request, response);
}else{
request.setAttribute("msg", "登录失败");
request.getRequestDispatcher("/loginsuccess.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Demo</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="demoServlet" method="post">
<input type="text" name="loginname"/><br/>
<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
登录信息页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="标签库地址"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'loginsuccess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
${msg }
</body>
</html>

需要介绍一下:登录信息的这个页面中的${msg }是使用jstl标签,需要在jsp页面中导入jstl标签库,使用这个标签库可以节省很多代码量。

‘贰’ java web实现简单的用户登录需要哪些技术

简单的话,就是jsp页面有个用户信息表单,点击提交就跑到后台servlet
然后后台就校验用户身份信息,信息可以从数据库拿,用jdbc连接数据库最简单了
完了之后就用response响应请求,或者是发request请求新的登录成功页面jsp

‘叁’ java 爬虫登陆网页

最简单的办法就是 在论坛页面审查元素,找到登录按钮,找到他的action,把它传到你的程序里面就好了,比如我现在回答你的问题,下面的提交回答按钮也会有一个action

‘肆’ 如何用java实现登陆网站--不需打开浏览器

public static void loginBai() {
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://www..com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www..com");
httpurlconnection.connect();

String cookie0 = httpurlconnection.getHeaderField("Set-Cookie");

System.out.println(cookie0);//打印出cookie
httpurlconnection.disconnect();
// String cookie0 =
// "BAIDUID=:FG=1;BDSTAT=;
// BDUSE=deleted";
url = new URL("http://passport..com/?login");
String strPost = "username=xxxxxx&password=yyyyyyy&mem_pass=on";
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setFollowRedirects(true);
httpurlconnection.setInstanceFollowRedirects(true);
httpurlconnection.setDoOutput(true); // 需要向服务器写数据
httpurlconnection.setDoInput(true); //
httpurlconnection.setUseCaches(false); // 获得服务器最新的信息
httpurlconnection.setAllowUserInteraction(false);
httpurlconnection.setRequestMethod("POST");
httpurlconnection
.addRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");
httpurlconnection
.setRequestProperty("Referer",
"http://passport..com/?login&tpl=mn&u=http%3A//www..com/");
httpurlconnection.setRequestProperty("Accept-Language", "zh-cn");
httpurlconnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpurlconnection.setRequestProperty("Accept-Encoding",
"gzip, deflate");
httpurlconnection
.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;MEGAUPLOAD 1.0)");
httpurlconnection.setRequestProperty("Host", "passport..com");
httpurlconnection.setRequestProperty("Content-Length", strPost
.length()
+ "");
httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
httpurlconnection.setRequestProperty("Cookie", cookie0);
httpurlconnection.getOutputStream().write(strPost.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
httpurlconnection.connect();
int code = httpurlconnection.getResponseCode();
System.out.println("code " + code);
String cookie1 = httpurlconnection.getHeaderField("Set-Cookie");
System.out.print(cookie0 + "; " + cookie1);
httpurlconnection.disconnect();
url = new URL("http://www..com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www..com");
httpurlconnection.setRequestProperty("Cookie", cookie0 + "; "
+ cookie1);
httpurlconnection.connect();
InputStream urlStream = httpurlconnection.getInputStream();
BufferedInputStream buff = new BufferedInputStream(urlStream);
Reader r = new InputStreamReader(buff, "gbk");
BufferedReader br = new BufferedReader(r);
StringBuffer strHtml = new StringBuffer("");
String strLine = null;
while ((strLine = br.readLine()) != null) {
strHtml.append(strLine + "\r\n");
}
System.out.print(strHtml.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
}

呵呵,改了下程序中错误的地方。学习了,原来这样也可以。
以前只知道可以这样做,还从来没有花过心思去做过。
阅读了些代码,真是获益匪浅啊。

程序可以运行了,用户名和密码改为你的帐户就可以直接运行了。可以使用,但不能一直使用啊,如果用多了网络给你个验证码,就是神仙也不行了。

‘伍’ 如何用JAVA编写一个简单用户登陆界面

什么都不说了 直接给你代码吧x0dx0apackage com.moliying.ui;x0dx0aimport java.awt.BorderLayout;x0dx0aimport java.awt.Container;x0dx0aimport java.awt.FlowLayout;x0dx0aimport java.awt.List;x0dx0aimport java.awt.event.ActionEvent;x0dx0aimport java.awt.event.ActionListener;x0dx0aimport java.io.BufferedWriter;x0dx0aimport java.io.FileOutputStream;x0dx0aimport java.io.OutputStreamWriter;x0dx0aimport java.util.ArrayList;x0dx0aimport java.util.Arrays;x0dx0aimport javax.swing.JButton;x0dx0aimport javax.swing.JFrame;x0dx0aimport javax.swing.JLabel;x0dx0aimport javax.swing.JPanel;x0dx0aimport javax.swing.JPasswordField;x0dx0aimport javax.swing.JTextField;x0dx0apublic class Login {x0dx0aprivate JFrame frame = new JFrame("登录");x0dx0aprivate Container c = frame.getContentPane();x0dx0aprivate JTextField username = new JTextField();x0dx0aprivate JPasswordField password = new JPasswordField();x0dx0aprivate JButton ok = new JButton("确定");x0dx0aprivate JButton cancel = new JButton("取消");x0dx0apublic Login() {x0dx0aframe.setSize(300, 200);x0dx0aframe.setBounds(450, 300, 300, 200);x0dx0ac.setLayout(new BorderLayout());x0dx0ainitFrame();x0dx0aframe.setVisible(true);x0dx0a}x0dx0aprivate void initFrame() {x0dx0a// 顶部x0dx0aJPanel titlePanel = new JPanel();x0dx0atitlePanel.setLayout(new FlowLayout());x0dx0atitlePanel.add(new JLabel("系统管理员登录"));x0dx0ac.add(titlePanel, "North");x0dx0a// 中部表单x0dx0aJPanel fieldPanel = new JPanel();x0dx0afieldPanel.setLayout(null);x0dx0aJLabel a1 = new JLabel("用户名:");x0dx0aa1.setBounds(50, 20, 50, 20);x0dx0aJLabel a2 = new JLabel("密 码:");x0dx0aa2.setBounds(50, 60, 50, 20);x0dx0afieldPanel.add(a1);x0dx0afieldPanel.add(a2);x0dx0ausername.setBounds(110, 20, 120, 20);x0dx0apassword.setBounds(110, 60, 120, 20);x0dx0afieldPanel.add(username);x0dx0afieldPanel.add(password);x0dx0ac.add(fieldPanel, "Center");x0dx0a// 底部按钮x0dx0aJPanel buttonPanel = new JPanel();x0dx0abuttonPanel.setLayout(new FlowLayout());x0dx0abuttonPanel.add(ok);x0dx0abuttonPanel.add(cancel);x0dx0ac.add(buttonPanel, "South");x0dx0ax0dx0aok.addActionListener(new ActionListener() {x0dx0ax0dx0a@Overridex0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aSystem.out.println(username.getText().toString());x0dx0a}x0dx0a});x0dx0ax0dx0acancel.addActionListener(new ActionListener() {x0dx0ax0dx0a@Overridex0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aframe.setVisible(false);x0dx0a}x0dx0a});x0dx0a}x0dx0apublic static void main(String[] args) {x0dx0a//new Login();x0dx0ax0dx0aString ss = "abbabbbaabbbccba";x0dx0ax0dx0aSystem.out.println(ss.split("b").length);x0dx0ax0dx0a}x0dx0a}

阅读全文

与java网页登陆相关的资料

热点内容
java互联网公司 浏览:68
对弈下象棋的app哪里好 浏览:705
有什么食谱app推荐 浏览:469
python实现动态口令 浏览:823
我的世界电脑服务器地址怎么添加 浏览:850
传奇地图怎么加密到pak 浏览:977
linux删除mysql用户 浏览:755
图案设计pdf 浏览:584
pdf编辑器在线 浏览:471
华为云云耀服务器如何关机 浏览:994
数字加密的历史 浏览:613
宏杰文件夹打不开 浏览:819
施工日记app哪个好 浏览:566
什么是压缩机的排气量 浏览:538
在哪个app可以预约一科考试 浏览:634
易语言vmp加壳源码 浏览:513
阅读前端框架源码 浏览:14
我的世界命令方块传送指令 浏览:545
不能用start命令打开xp 浏览:925
text命令 浏览:30