eclipse 用于计算器的 Servlet 和 html 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25078140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet and html form for calculator
提问by Vinit Saxena
I want to make calculator which basically add, subtract, multiply and divide two numbers. For achieving this first of all i have designed a form in HTML and feel desire to calculate the answer on server so i have written a code on servlet but when i hit the submit button of my form it will do nothing. Note: i am working eclipse so you are requested to answer my question with respect to eclipse. Calculator.java:
我想做一个计算器,它基本上可以加、减、乘、除两个数字。为了首先实现这一点,我设计了一个 HTML 表单,并希望在服务器上计算答案,所以我在 servlet 上编写了一个代码,但是当我点击表单的提交按钮时,它什么也不做。注意:我正在 eclipse 工作,所以请您回答我关于 eclipse 的问题。计算器.java:
package mypackage;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Calculator extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
int a1= Integer.parseInt(request.getParameter("n1"));
int a2= Integer.parseInt(request.getParameter("n2"));
if(request.getParameter("r1")!=null)
{
out.println("<h1>Addition</h1>"+(a1+a2));
}
if(request.getParameter("r2")!=null)
{
out.println("<h1>Substraction</h1>"+(a1-a2));
}
if(request.getParameter("r3")!=null)
{
out.println("<h1>Multiplication</h1>"+(a1*a2));
}if(request.getParameter("r1")!=null)
{
out.println("<h1>Division</h1>"+(a1/a2));
}
}
catch(Exception e)
{
}
}
}
index.html
索引.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">
<title>Calculator</title>
</head>
<body>
<h1 style="text_align=center">Calculator</h1>
<form method="get" action="/Servlet">
<label>first number:</label>
<input type="text" name="n1" />
<br />
<label>Second number : </label>
<input type="text" name="n2" />
<br />
<div>
<label>
<input type="radio" name="r1" value="add" />addition
<br />
</label>
<input type="radio" name="r2" value="sub" />subtraction
<br />
<input type="radio" name="r3" value="mul" />multiplication
<br />
<input type="radio" name="r4" value="div" />division
<br />
</div>
<input type="button" value="submit" />
</form>
</body>
</html>
web.xml:
网页.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>Servlet</display-name>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>mypackage.Calculator</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/firstHomePage</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
回答by Nivedita
You have written url-pattern 'firstHomePage' in web.xml for servlet name Calculator
您已在 web.xml 中为 servlet 名称计算器编写了 url-pattern 'firstHomePage'
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/firstHomePage</url-pattern>
</servlet-mapping>
so that what you should write in form action i.e.
所以你应该在表单操作中写什么,即
<form action="firstHomePage" method="get">
(you don't write / in form action)
(你不写/在形式动作)
whatever you write inside form action is checked in the url-pattern of all servlets mappings when a match is found the respective servlet name and the corresponding servlet class is searched for. That's how it works.
当找到匹配的相应 servlet 名称并搜索相应的 servlet 类时,在所有 servlet 映射的 url-pattern 中检查您在表单操作中写入的任何内容。这就是它的工作原理。
Hope you got your answer :)
希望你得到你的答案:)
UPDATE: write
更新:写
<input type="submit">
NOT BUTTONelse your form won't submit.
If you are using button
then you will have to write some javascript.
NOT BUTTON否则您的表单将不会提交。如果您正在使用, button
那么您将不得不编写一些 javascript。