用java servlets 写两个网页 第一页要求输入姓名和电话 有一个save的button 按了botton后 第二页显示 你好

显示 第二页显示你好,XXXX 你的电话是,XXXX 谢谢啦!
2024-12-25 14:35:32
推荐回答(2个)
回答1:

我用纯servlets写了一下,你可以看看:
第一个servlet,Index.java:

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Index extends HttpServlet {

public Index() {
super();
}
public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter pw = response.getWriter();
pw.println("");
pw.println("");
pw.println("

");
pw.println("请输入您的姓名
");
pw.println("请输入您电话号码
");
pw.println("");
pw.println("
");
pw.println("");
pw.println("");

}

public void init() throws ServletException {
}

}
第二个Success.java:

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Success extends HttpServlet {

public Success() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String name = request.getParameter("name");
String phone = request.getParameter("phone");
response.setContentType("text/html;charset=gbk");
PrintWriter pw = response.getWriter();
pw.println("");
pw.println("");
pw.println("欢迎您"+name+"
");
pw.println("您的电话号码是"+phone+"
");
pw.println("");
pw.println("");
}

public void init() throws ServletException {

}

}
web.xml中的配置如下:


xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

This is the description of my J2EE component
This is the display name of my J2EE component
Index
test.Index


This is the description of my J2EE component
This is the display name of my J2EE component
Success
test.Success



Index
/index


Success
/success


index.jsp


---希望对你有帮助。

回答2:

final long serialVersionUID = 1L;
Image imgs[]=new Image[5];
repaint();
改后代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PlayAudio extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
Image imgs[]=new Image[5];
Button b2;
public void init() {
setLayout(new FlowLayout()); // 使用布局管理器
imgs[0]=getImage(getCodeBase(),"1.jpg");
imgs[1]=getImage(getCodeBase(),"2.jpg");
imgs[2]=getImage(getCodeBase(),"3.jpg");
imgs[3]=getImage(getCodeBase(),"4.jpg");
imgs[4]=getImage(getCodeBase(),"5.jpg");
b2=new Button("下一张"); // 定义Play按钮
b2.addActionListener(this); // 给Play按钮添加一个监听事件
add(b2);
}
private static int count = 0;
public void actionPerformed(ActionEvent e)
{
//if(e.getSource() == b2)
//{
count=++count%5;
repaint();
//}
}
public void paint(Graphics g)
{

g.drawImage(imgs[count],0,40,this);
}
}
==============================
g.drawImage("m"+count,0,40,this);
这个地方需要传进image类型的对象:m"+count是字符串类型