用于计算器的Servlet和html表单 [英] Servlet and html form for calculator

查看:156
本文介绍了用于计算器的Servlet和html表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使计算器基本上加,减,乘,分两个数字。为了实现这一点,我首先设计了一个HTML表单,并希望在服务器上计算答案,所以我在servlet上编写了一个代码,但是当我点击我的表单的提交按钮时,它什么都不做。
注意:我正在工作eclipse,所以你被要求回答我关于eclipse的问题。
Calculator.java:

  package mypackage; 

import java.io. *;
import javax.servlet。*;
import javax.servlet.http。*;

public class Calculator扩展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>+ -a2));
}
if(request.getParameter(r3)!= null)
{
out.println(< h1>乘法< / h1>+ *a2));
} if(request.getParameter(r1)!= null)
{
out.println(< h1> Division< / h1>+(a1 / a2)) ;
}
}
catch(异常e)
{

}
}
}

index.html

  ;!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\"> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>计算器< / title>
< / head>
< body>
< h1 style =text_align = center>计算器< / h1>
< form method =getaction =/ Servlet>
< label>第一个数字:< / label>
< input type =textname =n1/>
< br />
< label>第二个数字:< / label>
< input type =textname =n2/>
< br />
< div>
< label>
< input type =radioname =r1value =add/>追加
< br />
< / label>
< input type =radioname =r2value =sub/> subtraction
< br />
< input type =radioname =r3value =mul/>乘法
< br />
< input type =radioname =r4value =div/> division
< br />
< / div>
< input type =buttonvalue =submit/>
< / form>
< / body>
< / html>

web.xml:

 <?xml version =1.0encoding =UTF-8?> 
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexmlns :web =http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdxsi:schemaLocation =http://java.sun.com/xml/ns/javaee http:// java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_IDversion =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>


解决方案

你已经在网页上写了url-pattern'firstHomePage' .xml for servlet name Calculator

 < servlet-mapping> 
< servlet-name> Servlet< / servlet-name>
< url-pattern> / firstHomePage< / url-pattern>
< / servlet-mapping>

所以你应该在表单动作中写,即

 < form action =firstHomePagemethod =get> 

(你不写/表单动作)



无论您写入内部表单操作是否在所有servlet映射的url模式中进行检查,当找到匹配时,将搜索相应的servlet名称和相应的servlet类。这是它的工作原理。



希望你得到你的答案:)

更新:写

 < input type =submit> 

NOT BUTTON 否则您的表单将不会提交。
如果您使用按钮,那么您将不得不写一些javascript。


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:

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

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

解决方案

You have written url-pattern 'firstHomePage' in web.xml for servlet name Calculator

        <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.

Hope you got your answer :)

UPDATE: write

      <input type="submit">

NOT BUTTON else your form won't submit. If you are using button then you will have to write some javascript.

这篇关于用于计算器的Servlet和html表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆