form.htm
<form action="formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>FormServlet.java
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;public class FormServlet extends HttpServlet
{
private ServletConfig config = null ;
public void init(ServletConfig config) throws ServletException
{
this.config = config ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doPost(req,resp) ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
String name = req.getParameter("uname") ;
// 取得application对象
ServletContext app = this.config.getServletContext() ;
app.setAttribute("addr","www.MLDN.cn") ;
// 取得一个session对象
HttpSession session = req.getSession() ;
session.setAttribute("sname",name) ;
System.out.println("** Servlet doPost处理提交参数 ...") ;
System.out.println("name = "+name) ;
// 重定向
resp.sendRedirect("demo.jsp") ;
}
};/*
<servlet>
<servlet-name>form</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>form</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
*/
demo.jsp
<h1><%=session.getAttribute("sname")%></h1><h1><%=getServletContext().getAttribute("addr")%></h1>
<h1><%=application.getAttribute("addr")%></h1>
tomcat已经启动
按提交以后地址栏转到:file:///C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/webapps/daima/formServletservletinputsessionapplication
<form action="formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>FormServlet.java
package cn.mldn.lxh.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;public class FormServlet extends HttpServlet
{
private ServletConfig config = null ;
public void init(ServletConfig config) throws ServletException
{
this.config = config ;
}
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
this.doPost(req,resp) ;
}
// 处理post请求
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
{
String name = req.getParameter("uname") ;
// 取得application对象
ServletContext app = this.config.getServletContext() ;
app.setAttribute("addr","www.MLDN.cn") ;
// 取得一个session对象
HttpSession session = req.getSession() ;
session.setAttribute("sname",name) ;
System.out.println("** Servlet doPost处理提交参数 ...") ;
System.out.println("name = "+name) ;
// 重定向
resp.sendRedirect("demo.jsp") ;
}
};/*
<servlet>
<servlet-name>form</servlet-name>
<servlet-class>cn.mldn.lxh.servlet.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>form</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
*/
demo.jsp
<h1><%=session.getAttribute("sname")%></h1><h1><%=getServletContext().getAttribute("addr")%></h1>
<h1><%=application.getAttribute("addr")%></h1>
tomcat已经启动
按提交以后地址栏转到:file:///C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/webapps/daima/formServletservletinputsessionapplication
比较烦这种
如:resp.sendRedirect("../../demo.jsp") ;// 重定向
resp.sendRedirect("demo.jsp") ;
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>然后再修改你的form<form action="<%=basePath%>formServlet" method="post">
用户名:<input type="text" name="uname">
<input type="submit" value="提交">
</form>
你看一下我的代码,我就是用的你的代码,配了一下环境,要不你用我的代码试试index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="formServlet" method="post">
用户名:
<input type="text" name="uname">
<input type="submit" value="提交">
</form>
</body>
</html>
demo.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'demo.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1><%=session.getAttribute("sname")%></h1>
<h1><%=getServletContext().getAttribute("addr")%></h1>
<h1><%=application.getAttribute("addr")%></h1>
</body>
</html>
FormServlet .javapackage com.csdn.test;import java.io.IOException;import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;public class FormServlet extends HttpServlet { private ServletConfig config = null; public void init(ServletConfig config) throws ServletException {
this.config = config;
} // 表示处理get请求
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
this.doPost(req, resp);
} // 处理post请求
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
String name = req.getParameter("uname");
// 取得application对象
ServletContext app = this.config.getServletContext();
app.setAttribute("addr", "www.MLDN.cn");
// 取得一个session对象
HttpSession session = req.getSession();
session.setAttribute("sname", name);
System.out.println("** Servlet doPost处理提交参数 ...");
System.out.println("name = " + name);
// 重定向
resp.sendRedirect("demo.jsp");
}}
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-name>FormServlet</servlet-name>
<servlet-class>com.csdn.test.FormServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>FormServlet</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
说明重定向resp.sendRedirect("demo.jsp") 根本就没成功,因为重定向的时候,浏览器地址栏是会改变的
这种情形是不是servlet都没进去如果是的话,直接访问formServlet行不行
http://127.0.0.1:8080/daima/formServlet