从jsp页面提交数据到servlet页面问题
2012-09-22 19:02
295 查看
input.jsp
此处值得注意的是action的值应该<%=request.getContextPath()%>/ShowServlet,其中<%=request.getContextPath()%>代表的是当前项目,/ShowServlet代表的是web.xml中配置的url-pattern。(表示之前不知道,找了很久……
ShowServlet.java
<%@ 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> <form action="<%=request.getContextPath()%>/ShowServlet"> <table> <tr> <td>用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="pass"></td> </tr> <tr> <td><input type="submit" value="登录"></td> <td><input type="reset" value="取消"></td> </tr> </table> </form> </body> </html>
此处值得注意的是action的值应该<%=request.getContextPath()%>/ShowServlet,其中<%=request.getContextPath()%>代表的是当前项目,/ShowServlet代表的是web.xml中配置的url-pattern。(表示之前不知道,找了很久……
ShowServlet.java
package com.cn.student; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShowServlet extends HttpServlet { PrintWriter pw = null; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("gb2312"); resp.setContentType("text/html;charset=gb2312"); pw = resp.getWriter(); String name = req.getParameter("username"); String pass = req.getParameter("pass"); pw.println("你输入的用户名为:" + name); pw.println("<br>"); pw.println("你输入的密码为:" + pass); } }web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>MyDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>com.cn.student.ShowServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/ShowServlet</url-pattern> </servlet-mapping> </web-app>
相关文章推荐
- 解决前台jsp页面提交中文数据以后,数据库中乱码问题??
- 商家后台页面密码修改问题—采用button提交数据,后端servlet一直取不到值
- ajax 提交数据到后台jsp页面及页面跳转问题
- Jsp servlet页面数据乱码问题解决(默认mysql数据库)
- JSP+Servlet学习经验1:解决form表单提交后相应页面中文乱码的问题
- 用Servlet处理表单提交的数据,然后再jsp页面中显示
- 关于一加载jsp页面就跳转到servlet获取数据然后再跳回jsp页面的问题
- SSM框架Jsp页面POST提交的中文数据保存到数据库变成乱码问题的分析
- Android客户端页面提交数据到tomcat的servlet插入mysql中的信息表遇到的中文乱码问题
- 【学习摘记】马士兵Servlet&JSP_课时6_htm页面提交数据并显示在新的页面上
- 解决刷新页面造成的数据重复提交问题
- 关于jsp表单提交中action所指向Servlet路径问题的总结
- jsp提交到Servlet报404错误问题(webroot下子目录)
- IE 8、9、10 兼容模式 使用 showModalDialog 返回数据后自动提交页面问题
- struts2的jsp页面提交后,命名空间重复出现的问题
- JSP 页面保存或者提交数据时,弹出浮层防止用户在保存过程中进行其他操作
- 记一次 从servlet传参数到jsp页面出现乱码问题的解决参考
- jsp页面提交到servlet案例
- 关于springMVC和传统servlet框架从jsp页面向后台请求url问题
- 从jsp页面发送ajax请求,servlet接受参数并返回json数据