Html нажатие кнопки не отправляет данные формы - PullRequest
0 голосов
/ 02 февраля 2012

Я тону в этом вопросе в течение нескольких часов .... Моя HTML-форма отображается, но когда я заполняю имя пользователя и нажимаю кнопку "Отправить", он не перенаправляется на servlet.but, когда я набираю URL как http://localhost:201/Practices/FirstPath?userName=achini, изменив метод формы HTML на "get" .. он работает хорошо ... PLZ, помогите мне

это мой код achHtml.html

<!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">

</head>
<body>
<form method="post" action="FirstPath">
    <input type="text" name="userName"/>
    <input type="button" value="Submit here"/>
</form>
</body>
</html>

это мой сервлет First.java

package com.achini.practice;

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

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

/**
 * Servlet implementation class First
 */
public class First extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String userName=request.getParameter("userName");
        out.println("Hello from the get method.!!!"+userName);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String userName=request.getParameter("userName");
        out.println("hello fom the post method"+userName);
    }

}

это файл 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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Practices</display-name>

  <servlet>
    <description></description>
    <display-name>First</display-name>
    <servlet-name>First</servlet-name>
    <servlet-class>com.achini.practice.First</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>First</servlet-name>
    <url-pattern>/FirstPath</url-pattern>
  </servlet-mapping>
</web-app>

1 Ответ

2 голосов
/ 02 февраля 2012

Ваш тип ввода должен быть submmit, а не кнопка.Как то так:

<form method="post" action="FirstPath">
    <input type="text" name="userName"/>
    <input type="submit" value="Submit here"/> <!-- NOTE THE CHANGE HERE -->
</form>
...