日期:2014-05-16  浏览次数:20400 次

jsp页面提交到servlet实例

jsp文件名jsp2.jsp

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp2
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
<form method="post" action="xianluserch">
<br/><br/>
<input type="text" value="sdfssssssssssss" >
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>

servlet
文件名 xianluserch.java
package web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class XianLuSerch extends HttpServlet {
??? private static final String CONTENT_TYPE = "text/html; charset=GBK";

??? //Initialize global variables
??? public void init() throws ServletException {
??? }

??? //Process the HTTP Get request
??? public void doGet(HttpServletRequest request, HttpServletResponse response) throws
??????????? ServletException, IOException {
??????? response.setContentType(CONTENT_TYPE);
??????? String ss=request.getParameter("tt");
??????? PrintWriter out = response.getWriter();
??????? out.println("<html>");
??????? out.println("<head><title>XianLuSerch</title></head>");
??????? out.println("<body bgcolor= "#ffffff ">");
??????? out.println("<p>The servlet has received a " + request.getMethod() +"ss"+
??????????????????? ". This is the reply.</p>");
??????? out.println("</body>");
??????? out.println("</html>");
??????? out.close();
??????? System.out.println("sddddddddddddddddddddddddddddddddddddddd");
??? }

??? //Process the HTTP Post request
??? public void doPost(HttpServletRequest request, HttpServletResponse response) throws
??????????? ServletException, IOException {
??????? response.setContentType(CONTENT_TYPE);
??????? doGet(request, response);
??? }

??? //Clean up resources
??? public void destroy() {
??? }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
? <display-name>Web</display-name>


? <servlet>
??? <servlet-name>xianluserch</servlet-name>
??? <servlet-class>web.XianLuSerch</servlet-class>
? </servlet>
? <servlet-mapping>
??? <servlet-name>xianluserch</servlet-name>
??? <url-pattern>/xianluserch</url-pattern>
? </servlet-mapping>

? <servlet>
??? <description>Added by JBuilder to compile JSPs with debug info</description>
??? <servlet-name>debugjsp</servlet-name>
??? <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
??? <init-param>
????? <param-name>classdebuginfo</param-name>
????? <param-value>true</param-value>
??? </init-param>
??? <load-on-startup>3</load-on-startup>
? </servlet>
? <servlet-mapping>
??? <servlet-name>debugjsp</servlet-name>
??? <url-pattern>*.jsp</url-pattern>
? </servlet-mapping>
</web-app>
?
本篇文章来源于:http://edu.codepub.com/2009/0630/7600.php