利用servlet来实现动态页面的提交效果

在一个工程内新建一个html文件,设置好内容后,创建一个servlet文件,而html中的action必须和servlet保持一致;同时要注意乱码事件;

package webprojecthomework;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

public class GetGoodsServlet extends HttpServlet {

/**
* Constructor of the object.
*/
// public GetGoodsServlet() {
// super();
// }

/**
* Destruction of the servlet. <br>
*/
// public void destroy() {
// super.destroy();
// }

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@SuppressWarnings("null")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");//设置编码格式
response.setContentType("text/html;charset=UTF-8");
String goods = request.getParameter("goods");//获取文本框name值
String btn = request.getParameter("btn");
ArrayList<Goods> templist = new ArrayList<Goods>();//创建动态数组存放数据
for (int i = 0; i < GoodsData.goodslist.size(); i++) {
Goods tempgoods = GoodsData.goodslist.get(i);
String name = tempgoods.getName();
if (name.indexOf(goods) != -1) {
templist.add(tempgoods);
}
}
PrintWriter out = response.getWriter();
if (templist.size() == 0) {
out.println("没有符合要求的商品<br>");
} else {
out.println("<table border=1 width='600'>");
out.println("<tr>");
out.println("<td>编号</td>");
out.println("<td>名称</td>");
out.println("<td>品牌</td>");
out.println("<td>价格</td>");
out.println("<td>颜色</td>");
out.println("</tr>");
for (int i = 0; i < templist.size(); i++) {
Goods findgoods = templist.get(i);
out.println("<tr>");
out.println("<td>" + findgoods.getId() + "</td>");
out.println("<td>" + findgoods.getName() + "</td>");
out.println("<td>" + findgoods.getBrand() + "</td>");
out.println("<td>" + findgoods.getPrice() + "</td>");
out.println("<td>" + findgoods.getColor() + "</td>");
out.println("</tr>");
}
out.println("</table>");
}
out.println("</body>");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eclipse是一个强大的集成开发环境(IDE),可以用于开发基于Java的web应用程序。通过使用Eclipse中的Servlet程序,我们可以实现用户注册和用户登录的功能。 用户注册功能的实现: 1. 创建一个新的Servlet类,用于处理用户注册的相关逻辑。 2. 在Servlet中,创建一个处理POST请求的方法,并配置Servlet的访问路径。 3. 在处理方法中,获取用户提交的注册信息,例如用户名、密码和其他必要的信息。 4. 对用户提交的信息进行验证和校验,确保用户输入的信息符合要求。 5. 如果用户提交的信息合法,则将用户信息保存到数据库中或者其他持久化方式中。 6. 返回注册成功的页面或者提示信息,告知用户注册成功。 用户登录功能的实现: 1. 创建一个新的Servlet类,用于处理用户登录的逻辑。 2. 在Servlet中,创建一个处理POST请求的方法,并配置Servlet的访问路径。 3. 在处理方法中,获取用户提交的登录信息,例如用户名和密码。 4. 对用户提交的信息进行验证和校验,确保用户输入的信息符合要求。 5. 从数据库或者其他持久化方式中查询用户信息,并与用户输入的信息进行比对。 6. 如果用户输入的信息和数据库中的信息匹配,则标记用户为登录状态,并可以根据需要记录登录间等信息。 7. 返回登录成功的页面或者跳转到其他需要登录才能访问的页面。 以上是利用Eclipse中的Servlet程序实现用户注册和用户登录的基本步骤。在具体实现过程中,还需要考虑异常处理、安全性等方面的问题,以确保用户信息的安全和程序的稳定运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值