sverlet 如何将自定义的请求内容 返回客户端?

xzwsun 2009-04-14 04:53:56
我通过一个类在客户端向servlet 发出请求,Servlet 处理完后,返回 “处理成功”的字符串。

server 和 client 应如何写?
...全文
118 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
BAO_YY 2009-04-14
  • 打赏
  • 举报
回复
满详细的! 顶
huangan0301 2009-04-14
  • 打赏
  • 举报
回复
用EL标签,或者request.getAttribute("name");
AsheBin 2009-04-14
  • 打赏
  • 举报
回复
世界上真有这么执著的.......
xxm712 2009-04-14
  • 打赏
  • 举报
回复
richard_2010 2009-04-14
  • 打赏
  • 举报
回复
ls的太详细了。。。
yxc0433 2009-04-14
  • 打赏
  • 举报
回复
客户端写个一jsp页面
//提交你的表单,假设是a,b

<form action="myjsp.jsp" method = "post" >

please enter a:<input type="text" name="a">
please enter b:<input type="text" name="b">
<input type="submit" >

</form>

提交到web服务器交给servlet处理

package com.number.servlet;
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 Number extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
/*做你所要做的东西*/
PrintWriter out = response.getWriter();
out.println("处理成功")


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);

}

}




web.xml


<servlet>
<servlet-name>first</servlet-name>
<servlet-class>com.number.servlet.Number</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
applerockhool 2009-04-14
  • 打赏
  • 举报
回复
客户端代码:
<html>
<head>
<base href="<%=basePath%>">

<title>欢迎使用</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


</head>
<script language="javascript">
function validation(myform){
if(myform.name.value.length==0){
alert("请填写用户名!");
myform.name.focus();
return false;
}
if(myform.password.value.length==0){
alert("请填写密码!");
myform.password.focus();
return false;
}
}

function setFalse(){
if(document.form1.name.value.length!=0 && document.form1.password.value.length!=0){
document.getElementById("s").disabled=false;
}
}

function setTrue(){
if(document.form1.name.value.length!=0 && document.form1.password.value.length!=0){
document.getElementById("s").disabled=true;
}
}


</script>

<body>
<h1 align="center">
欢迎登陆
</h1>
<br>
<br>
<form id="form1" name="form1" method="post"
action="servlet/LoginServlet" onsubmit="return validation(this)">

<table align="center">

<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="name" onmouseout="setFalse()">
</td>
</tr>
<tr>
<td>
密 码:
</td>
<td>
<input type="password" name="password" onmouseout="setFalse()">
</td>
</tr>
<tr>
<td>
<input type="submit" value="登陆" id="s" disabled>
</td>
<td>
<input type="reset" value="重置" onclick="setTrue()">
</td>
</tr>
</table>

</form>
</body>
</html>
]

客户端servlet:
package com.handlewell.control;

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 Serv extends HttpServlet {

/**
* Constructor of the object.
*/
public Serv() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("处理成功!!");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}


62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧