Java要运行html代码,需要运行在服务器端,也就是servlet容器中,经过容器编译解析,返回html静态内容,示例如下:
公司主营业务:做网站、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出海城免费做网站回馈大家。
在servlet里面写就可以了
引入一系列包
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class Servlet1 extends HttpServlet {
public void doGet(ServletRequest req,ServletResponse res)throws ServletException, IOException{try{PrintWriter pw=res.getWriter();//在浏览器输出需要
pw.println("scriptscript");}catch(exception e){="" e.printstacktrace();="" 为发现调试错误}}}=""
/* Main.java
*
* Created on __DATE__, __TIME__
*/
import javax.swing.JOptionPane;
/**
*
* @author __USER__
*/
public class Main extends javax.swing.JFrame {
/** Creates new form Main */
public Main() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jPanel1 = new javax.swing.JPanel(); //标签
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton(); //登录按钮
jButton2 = new javax.swing.JButton(); //
jTextField1 = new javax.swing.JTextField(); //文本框
jTextField2 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //关闭时的操作,即退出程序
setTitle("登录界面"); //设置标题:欢迎使用股票管理系统
setResizable(false); //设置窗口不可调节大小
jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 18)); //设置标签的字体
jLabel1
.setText("您好,请输入信息");
jLabel2.setText("用户名");
jLabel3.setText("用户密码");
jButton1.setText("登录");
jButton1.addActionListener(new java.awt.event.ActionListener() { //采用匿名内部类来实现按钮监听功能
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("退出");
jButton2.addActionListener(new java.awt.event.ActionListener() { //同上
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout( //层次布局管理器,下面n长的代码,就是设置位置,自己看吧,呵呵,这个必须的,就不多写了
jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout
.setHorizontalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(159, Short.MAX_VALUE)
.addComponent(jLabel1).addGap(140, 140,
140))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(110, 110, 110)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(54, 54, 54)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jTextField2,
javax.swing.GroupLayout.DEFAULT_SIZE,
191,
Short.MAX_VALUE)
.addGap(
112,
112,
112))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
191,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(141, 141, 141)
.addComponent(
jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE,
68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(89, 89, 89)
.addComponent(
jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE,
72,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(145, Short.MAX_VALUE)));
jPanel1Layout
.setVerticalGroup(jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(55, 55, 55)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(
jTextField2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43)
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE,
33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE,
33,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(48, Short.MAX_VALUE)));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addContainerGap().addComponent(
jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addContainerGap().addComponent(
jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
pack();
setLocationRelativeTo(null); //居中
}// /editor-fold
//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //事件处理,不建议这样做
// TODO add your handling code here:
String name = this.jTextField1.getText().trim(); //获得用户名,去空格
String password = this.jTextField2.getText().trim(); //获得密码,去空格
if (name.equals("admin") password.equals("123456")) { //如果符合条件.....其实这里应该去和数据库关联,这是死代码
// Showinformation sf=new Showinformation(); //我估计是另外一个框架,就是登录成功之后的框架
// sf.setVisible(true);
// sf.show(); //这个方法就直接秒杀,不用了,因为sf.setVisible(true)已经被调用了,这里就多次一举了
} else { //用户名或者密码验证失败
JOptionPane.showMessageDialog(this, "用户名或者密码错误啦~", "提示", //提示框
JOptionPane.INFORMATION_MESSAGE);
this.jTextField1.setText(""); //清空输入框
this.jTextField2.setText("");
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { //事件处理,其实可以将jButton1ActionPerformed放在一起处理,只要获得按钮的标签内容 (JButton)(evt.getSuorse()).getText()就做对比就ko了
// TODO add your handling code here:
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { //好方法,呵呵使用该方式的原因是:awt是单线程模式的,所有awt的组件只能在(推荐方式)事件处理线程中访问,从而保证组件状态的可确定性。java核心编程技术中每个实例基本都用到了
public void run() {
new Main().setVisible(true);
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;}
// End of variables declaration//GEN-END:variables
你修改一下吧,从记事本获得用户名判断一下就行了
代码:
package FrameText;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame {
JButton jbtwo ,jbHello, jbBye;
public FrameDemo() {
setLayout(new GridLayout(3, 1));// 3行1列布局
JPanel jp1 = new JPanel();// 第一行
JButton jb1 = new JButton("第一个按钮");
JButton jb2 = new JButton("第二个按钮");
JButton jb3 = new JButton("第三个按钮");
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
add(jp1);
JPanel jp2 = new JPanel();// 第二行
JButton jbtwo = new JButton("第四个按钮");
jp2.add(jbtwo);
add(jp2);
JPanel jp3 = new JPanel();// 第三行
jbHello = new JButton("第五个按钮");
jbBye = new JButton("第六个按钮");
jp3.add(jbHello);
jp3.add(jbBye);
add(jp3);
setSize(380, 180);
setTitle("窗口");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//点击按钮后响应
public static void main(String[] args) {
new FrameDemo();
}
}
jsp中写java代码成为scriptlet,写在%%之间就可以了。
Scriptlet是包含在%和%之间的Java代码,在Web容器处理JSP页面时执行,通常会产生输出,并将输出发送到客户的输出流里。Scriptlet除了不能定义类和方法、不能用import引入类外,可以包含任何有效的Java代码。(Java类在Jsp外部定义,可用page指令的import属性引入,也可以Java Bean的形式使用。Java中的方法必须在类内定义,但Jsp允许使用声明定义方法。窗体(GUI)设计代码在Jsp中无效)。
Scriptlet例程:
%@ page contentType="text/html; charset=gb2312" %
html
head
titleJSP基本语法/title
/head
body
h1Scriptlet示例页面/h1
table border="1"
caption乘法口诀表/caption
%-- 在网页中嵌入Java代码的主要方法 --%
%
for(int i=1; i=9; i++) {
int j=1;
//out是JSP的一个内部对象,print方法用于向客户端输出数据
out.println("tr");
for(; j=i; j++) {
out.print("td" + j + "*" + i + "=" + j*i + "/td");
}
for(;j=9;j++) {
out.print("td /td");
}
out.println("/tr");
}
%
/table
/body
/html