我是初学者,给我写一个servlet服务,就是我输入一个地址就可以访问一个html页面,然后我可以通过这个页面和servlet之间进行信息交换,(弄个2个button 1个按下之后显示一句话,另外一个改变servlet里面的某个值后显示,多了我看不懂)需要给我servlet xml html等全部代码 和所有目录和文件存放的路径(请从一个初学者的角度出发), 上面写上注释,service() 方法 doGet()方法doPost()方法以外最好别写上了~~· 只要成功了 我一定追加分···
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SimpleServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response); //当是get提交时也转到post方法,一般情况下他们的业务流程都一样
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value=request.getParameter("txtValue"); //获取页面name为txtValue文本框的值
value="我被servlet改变了"; //重新赋值
request.setAttribute("value", value); //存入request中,便于在页面上使用
//跳转到index页面
request.getRequestDispatcher("index.jsp").forward(request, response);
}}
2.web.XML<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<!-- servlet配置信息 -->
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>com.servlets.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/SimpleServlet</url-pattern> <!-- 路径 -->
</servlet-mapping>
<!-- 默认页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>3.jsp页面<%@ page language="java" contentType="text/html; charset=GBK"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="SimpleServlet">
<input type="text" name="txtValue" value="<%=request.getAttribute("value")==null?"我是初始值":request.getAttribute("value")%>"/><br/>
<input type="button" value="按钮1显示一句话" onclick="alert('你要访问的servlet是SimpleServlet');">
<input type="submit" value="按钮2改变值">
</form>
</body>
</html>
4.文件路径截图
还是很小
换个连接
http://p5.images22.51img1.com/6000/liutaixin/55c99c7ec6512488ce286acee760f15a.jpg