你可以在 jsp 中做所有事情。使用 JAVA MVC 框架
即使没有,您也可以使用Servlet执行简单的反手操作,
或者您可以使用Java Beans,这是 Java bean 的简单业务逻辑实现示例,请参考http://www.tutorialspoint.com/jsp/jsp_java_beans.htm
下面给出了Servlet的示例
索引.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="add" method="post">
Value 1:<input type="text" name="val1" id="val1"/><br>
Value 2:<input type="text" name="val2" id="val2"/><br>
<input type="submit" value="Submit"/><br>
</form>
<%String sum="";
sum = (String)request.getAttribute("val3"); %>
<input type="text" value="<%=sum%>" />
</body>
</html>
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>add</servlet-name>
<servlet-class>controller.add</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
添加.java
package controller;
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;
public class add extends HttpServlet {
String val3;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String val1=request.getParameter("val1");
String val2=request.getParameter("val2");
if(val1 != null && val2 != null)
val3=""+(Integer.parseInt(val1)+Integer.parseInt(val2));
else
val3="";
request.setAttribute("val3",val3);
request.getRequestDispatcher("index.jsp").forward(request, response);
try {
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}