<script>
var xhr = null;
function createRequest() {
if(window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} else {
xhr = new XMLHttpRequest();
}
}
function sendRequest(url, param, callback, method) {
createRequest();
var httpMethod = (method!='POST' && method!='post') ? 'GET' : 'POST';
var httpParam = (param==null || param=='') ? null : param;
var httpURL = url;
if(httpMethod=='GET' && httpParam!=null) {
httpURL = httpURL+"?"+httpParam; // test.jsp?idx=12
}
xhr.onreadystatechange = callback;
xhr.open(httpMethod, httpURL, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form- urlencoded");
xhr.send(httpMethod=='POST' ? httpParam : null);
}
function valuecheck() {
var value;
for (var i = 0; i < document.data.radio_rate.length; i++) {
if (document.data.radio_rate[i].checked) {
value = document.data.radio_rate[i].value;
}
}
var param = "value=" + encodeURIComponent(value) + "&mid=" + encodeURIComponent(<%=bean.getMovie_Id()%>);
endRequest("/Rate", param, idResult, "post");
}
function idResult() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert("inserted");
}
}
}
</script>
<form name = "data" method = "post">
<p>
<font size = "4">
Rate: 1<input name="radio_rate" type="radio" value="1" onclick="valuecheck()"/>
2<input name="radio_rate" type="radio" value="2" onclick="valuecheck()"/>
3<input name="radio_rate" type="radio" value="3" onclick="valuecheck()"/>
4<input name="radio_rate" type="radio" value="4" onclick="valuecheck()"/>
5<input name="radio_rate" type="radio" value="5" onclick="valuecheck()"/>
</font>
</p>
</form>
web.xml
<servlet>
<servlet-name>RateServlet</servlet-name>
<servlet-class>lasec.servlet.RateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RateServlet</servlet-name>
<url-pattern>/Rate</url-pattern>
</servlet-mapping>
package lasec.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lasec.util.DBManager;
/**
* Servlet implementation class RateServlet
*/
@WebServlet("/RateServlet")
public class RateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private int serviceCounter = 0;
private boolean shuttingDown;
private DBManager manage;
/**
* @see Servlet#init(ServletConfig)
*/
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
manage = new DBManager();
System.out.println("Init()");
}
protected synchronized void enteringServiceMethod() {
serviceCounter++;
}
protected synchronized void leavingServiceMethod() {
serviceCounter--;
}
protected synchronized int numServices() {
return serviceCounter;
}
protected synchronized void setShuttingDown(boolean flag) {
shuttingDown = flag;
}
protected synchronized boolean isShuttingDown() {
return shuttingDown;
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
if (numServices() > 0) {
setShuttingDown(true);
}
while (numServices() > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
manage = null;
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
enteringServiceMethod();
try {
super.service(request, response);
} finally {
leavingServiceMethod();
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String mid = request.getParameter("mid");
String value = request.getParameter("value");
HttpSession session = request.getSession(true);
String uid = (String) session.getAttribute("uid");
try {
manage.insertMovieRate(uid, mid, value);
out.print("success");
} catch (Exception e) {
out.print("fail");
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request, response);
}
}
If I press the radio button, I want to send the values to the servlet so it can perform database jobs. However, it does not work. localhost:8080/<Project name>/Rate?uid=&mid= works.
Is something wrong? I am using JBoss 6.0 AS.