gpt4 book ai didi

java - JSP获取html类型=数字输入字段的值

转载 作者:行者123 更新时间:2023-12-02 11:31:28 24 4
gpt4 key购买 nike

所以,我必须制作一种提供工具作为学习JSP的作业,其基本原理是客户端进入索引站点并输入他的数据,然后他被重定向到显示他的第二个页面一切都经过计算的完整报价。

表格如下所示:

<form method="POST" action="offer.jsp">
<table>
<tr>
<td>Name:</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td>Addresse:</td><td><input type="text" name="address"></td>
</tr>
<tr>
<td>Boden in m²:</td><td><input type="number" name="area" min="1" value="10"></td>
</tr>
<tr>
<td>Anzahl der R&auml;ume mit bis zu 15m²:</td><td><input type="number" name="rooms_15" min="0"</td>
</tr>
<tr>
<td>Anzahl der R&auml;ume mit bis zu 25m²:</td><td><input type="number" name="rooms_25" min="0"></td>
</tr>
<tr>
<td>Anzahl der R&auml;ume mit bis zu 40m²:</td><td><input type="number" name="rooms_40" min="0"></td>
</tr>
<tr>
<td>Anzahl der WC-R&auml;ume:</td><td><input type="number" name="toiletrooms" min="0"></td>
</tr>
<tr>
<td>Anzahl der WCs insgesamt:</td><td><input type="number" name="toilets" min="0"></td>
</tr>
<tr>
<td>Anzahl der B&auml;der:</td><td><input type="number" name="bathrooms" min="0"></td>
</tr>
<tr>
<td>Anzahl der K&uuml;chen:</td><td><input type="number" name="kitchens" min="0"></td>
</tr>
</table>
<br>
<input type="submit" value="Berechnen" onclick="<%
offer.setName(request.getParameter("clientname"));
offer.setAddress(request.getParameter("address"));
System.out.print("\n\n\n"+request.getParameter("area")+"\n\n\n"); // S.O.P. for testing reasons
// offer.setArea(request.getParameter("area"));
%>; location.href = 'offer.jsp'">
</form>

我只是为了测试而摆弄区域属性。计划是将所述输入的值设置为我的 JavaBean,但 request.getParameter("area"); 只是保留 null,即使我给它一个使用 html value="10" 的默认值。我已经发现输入需要采用某种形式才能工作(废话),但仅此而已。问题不在于我的 JavaBean 或类似的东西,因为正常的文本字段(名称和地址字段)工作得很好,并且在下一页上正确显示。

希望有人能帮助我。

此致,
斯克拉特领主

最佳答案

    **MVC Architecture with JSP and Servlet**

**Model**

public class StudentBean {

private int rno;
private String name;
private float fee;

public StudentBean() {

}

public int getRno() {
return rno;
}

public void setRno(int rno) {
this.rno = rno;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getFee() {
return fee;
}

public void setFee(float fee) {
this.fee = fee;
}

@Override
public String toString() {
return "StudentBean [rno=" + rno + ", name=" + name + ", fee=" + fee + "]";
}

}

**View**

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>

<form method="get" action="StudentController">

Rno: <input type="text" name="rno"/> <br/>
Name: <input type="text" name="name"/> <br/>
Fee: <input type="text" name="fee"/> <br/>
<input type="submit" name="btnSubmit" value="Submit"/>
</form>
</body>
</html>

**Controller**

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/StudentController")
public class StudentController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

StudentBean studentBean = new StudentBean();
studentBean.setRno(Integer.parseInt(request.getParameter("rno")));
studentBean.setName(request.getParameter("name"));
studentBean.setFee(Float.valueOf(request.getParameter("fee")));


System.out.println(studentBean);

request.getSession().setAttribute("studentObject", studentBean);
request.getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}

}

**another page, where bean is being used.**

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.loardscrat.controller.*" %>
<!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>Insert title here</title>
</head>
<body>

<h1>Hello</h1>
<%
StudentBean studentBean = (StudentBean) request.getSession().getAttribute("studentObject");
out.print(studentBean.getRno());
out.print(studentBean.getName());
out.print(studentBean.getFee());

%>
</body>
</html>

关于java - JSP获取html类型=数字输入字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49267402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com