
正文
java前端页面源代码 java web源代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java中如何根据一个网址获得该网页的源代码,急求
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
String urlString;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest(网址);
client.run();
}
public HttpTest(String urlString) {
this.urlString = urlString;
}
public void run() throws Exception {
//生成一个URL对象
URL url = new URL(urlString);
//打开URL
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//得到输入流,即获得了网页的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
.getInputStream()));
String line;
// 读取输入流的数据,并显示
while ((line = reader.readLine()) != null){
System.out.println(line);
}
}
}
相关问答
Q1: java图形界面设计实验,求源代码!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator01 extends JFrame implements ActionListener {
JPanel jp1,jp2;
JTextField jt1,jt2,jt3;
JButton btn_add,btn_sub,btn_clean;
public Calculator01() {
init();
}
public void init() {
setTitle("简易计算器");
setLocationRelativeTo(null);
setSize(600, 100);
jp1 = new JPanel();
jp2 = new JPanel();
add(jp1, BorderLayout.NORTH);
add(jp2,BorderLayout.SOUTH);
jt1 = new JTextField(15);
jp1.add(jt1);
jt2 = new JTextField(15);
jp1.add(jt2);
jt3 = new JTextField(15);
jp1.add(jt3);
btn_add = new JButton("+");
btn_add.addActionListener(this);
jp2.add(btn_add);
btn_sub = new JButton("-");
btn_sub.addActionListener(this);
jp2.add(btn_sub);
btn_clean = new JButton("清除");
btn_clean.addActionListener(this);
jp2.add(btn_clean);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn_clean)
{
jt1.setText("");
jt2.setText("");
jt3.setText("");
}
else
{
if (jt1.getText().equals("") || jt2.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "请在前两个框输入数字");
}
else
{
double number1=Double.parseDouble(jt1.getText());
double number2=Double.parseDouble(jt2.getText());
double result=0;
if (e.getSource() == btn_add)
{
result = number1 + number2;
}
else
{
result = number1 - number2;
}
jt3.setText(""+result);
}
}
}
}
Q2: 用java写一个网页输入url点击查询即可在下面显示网页源代码
import java.io.*;
import java.net.*;
public class Demo {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入要显示源码的地址:");
URL url = new URL(br.readLine());
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b))!=-1){
System.out.println(new String(b,0,len,"UTF-8"));
}
}
}
//下班,控制台的,网页的你稍微修改一下就OK啦。
Q3: java程序读取一个url页面的源代码
传入一个url,返回源代码; public static String getHTML(String url){// 获取指定URL的网页,返回网页内容的字符串,然后将此字符串存到文件即可 try { URL newUrl = new URL(url); URLConnection connect = newUrl.openConnection(); connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); DataInputStream dis = new DataInputStream(connect.getInputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8")); String html = ""; String readLine = null; while((readLine = in.readLine()) != null) { html = html + readLine; } in.close(); return html; }catch (MalformedURLException me){ System.out.println("MalformedURLException" + me); }catch (IOException ioe){ System.out.println("ioeException" + ioe); } return null; }
Q4: 怎样高效的阅读JavaWeb项目源代码
首先要理清楚代码结构和业务结构(应该有些文档或者大的流程图),这是阅读具体代码的前提。
阅读Java web项目的代码:
你需要找到
View层的代码:前端页面、图片、资源文件都在其中。
Controller层的代码:控制试图与模型层以及数据传递。
Service层的代码:业务逻辑。
Dao层的代码:数据库访问逻辑。
从web.xml - appcontext.xml - xxx
java前端页面源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java web源代码、java前端页面源代码的信息别忘了在本站进行查找喔。







