新手 servlet各位大侠
新手 servlet求助各位大侠
1、采用Servlet编写程序实现在页面上整数的加(Plus)和减(Minus)的功能,页面效果如下所示。
请输入两个整数:
操作数1:(文本) 操作数2:(文本)
plus minus
------解决方案--------------------
我帮你写了一下,代码如下:
1.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<body>
<form action="MyServlet" method="post">
<input type="text" name="a" value="${a}">
<select name="type">
<option value="Plus">+</option>
<option value="Minus">-</option>
</select>
<input type="text" name="b"value="${b}">
=<input type="text" value="${result}"><input type="submit" value="计算"/><br>
</form>
</body>
</html>
2.MyServlet.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int a=request.getParameter("a")==null?0:Integer.parseInt(request.getParameter("a"));
int b=request.getParameter("b")==null?0:Integer.parseInt(request.getParameter("b"));
String type=request.getParameter("type");
int result=0;
if("Plus".equals(type)){
result=a+b;
}else if("Minus".equals(type)){
result=a-b;
}
request.setAttribute("a", a);
request.setAttribute("b", b);
request.setAttribute("result", result);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
3.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>MyServlet</servlet-name>
<servlet-class>servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
------解决方案--------------------
1、采用Servlet编写程序实现在页面上整数的加(Plus)和减(Minus)的功能,页面效果如下所示。
请输入两个整数:
操作数1:(文本) 操作数2:(文本)
plus minus
------解决方案--------------------
我帮你写了一下,代码如下:
1.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<body>
<form action="MyServlet" method="post">
<input type="text" name="a" value="${a}">
<select name="type">
<option value="Plus">+</option>
<option value="Minus">-</option>
</select>
<input type="text" name="b"value="${b}">
=<input type="text" value="${result}"><input type="submit" value="计算"/><br>
</form>
</body>
</html>
2.MyServlet.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int a=request.getParameter("a")==null?0:Integer.parseInt(request.getParameter("a"));
int b=request.getParameter("b")==null?0:Integer.parseInt(request.getParameter("b"));
String type=request.getParameter("type");
int result=0;
if("Plus".equals(type)){
result=a+b;
}else if("Minus".equals(type)){
result=a-b;
}
request.setAttribute("a", a);
request.setAttribute("b", b);
request.setAttribute("result", result);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
3.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>MyServlet</servlet-name>
<servlet-class>servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
------解决方案--------------------
- Java code
package com.birdy.rmi; import java.rmi.Remote; public interface Calculator extends Remote{ public long add(long a , long b) throws java.rmi.RemoteException; public long sub(long a , long b) throws java.rmi.RemoteException; }