When writing projects, the data of the front-end page is often transferred to the back-end server. That is, there are several ways to pass values from jsp to Servlet.
Method of jsp passing value to servlet:
First method:
Transfer value through hyperlink.
Jsp page:
<a href="AServlet?password=传输内容">文本</a>
Servlet code:
String d = request.getParameter("password");
Second type:
Transmit through the form value.
jsp page:
<form action="LoginServlet"> <input type="text" name="username"> <input type="text" name="password"> <input type="submit" value="登录"> </form>
Servlet code:
String username = request.getParameter("username"); String password = request.getParameter("password");
The third type:
can also be passed through java code Pass value,
java fragment code, the servlet can only receive the content of session.setAttribute("testSession", "Hello session"), but cannot receive the content of the request. Use request.getSession().getAttribute("testSession") in the servlet to obtain the session content.
It is not recommended to write like this, it will be very troublesome to maintain in the later stage.
The above is the detailed content of How to pass value to servlet in jsp. For more information, please follow other related articles on the PHP Chinese website!