பயனர்:Natkeeran/யாவா சேர்வலற்சு

விக்கிநூல்கள் இலிருந்து

யாவா இன்று மிகவும் பரவலாகப் பயன்படுத்தப்படும் ஒரு நிரல் மொழி ஆகும். யாவா மொழியின் சேர்வலற்சு (Servlets) மற்றும் யே.எசு.பி என அறியப்படும் யாவா வழங்கிப் பக்கங்கள் (Java Server Pages) யாவா மொழியைப் பயன்படுத்தி வலைச் செயலியைகளை உருவாக்கப் பயன்படும் நுட்பங்கள் ஆகும். யே.எசு.பி எச்.ரி.எம்.எல் பக்கங்களில் இடப்படக் கூடியது. வலைப்பக்கங்கள் ஏற்றப்படும் பொழுது யே.எசு.பி கணிக்கப்படுகிறது. சேர்வலற்சுகள் தனியாக யாவாவில் எழுதப்பட்டவை. பொதுவான பயன்பாட்டில் காட்சிப்படுத்தல் தேவைகளுக்கு யே.எசு.பி யும், பின்தள கட்டுப்பாட்டு தர்க்க வேலைகளுக்கு சேர்வலற்சும் பயன்படுத்தப்படுகின்றன.

உலகே வணக்கம்[தொகு]

http://localhost:8080/HelloWorld/vanakkam

யாவா (HelloServlet.java)[தொகு]

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
	  res.setCharacterEncoding("UTF-8");
	  res.setContentType("text/html");
	  PrintWriter out = res.getWriter();
	  out.println("உலகே வணக்கம்");
          out.close();           
  }	  	  
}

web.xml[தொகு]

<!DOCTYPE web-app PUBLIC
  '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
  'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
  <servlet>
    <servlet-name>vanakkam</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>vanakkam</servlet-name>
    <url-pattern>/vanakkam</url-pattern>
  </servlet-mapping>
</web-app>

ஏயாக்சு[தொகு]

யே.எசு.பி/எச்.ரி.எம்.எல்[தொகு]

<html>
<head>
<title>JSP and Servlet using AJAX</title>
<script type="text/javascript">
 
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object
 
function ajaxFunction() {
  if(xmlhttp) {
  	var txtname = document.getElementById("txtname");
    xmlhttp.open("POST","vanakkam",true); //getname will be the servlet name
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("txtname=" + txtname.value); //Posting txtname to Servlet
  }
}
 
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}
</script>
<body>
<form name="myForm" method="POST" action="getname">
<table>
 <tr>
  <td>Enter Name</td>
  <td><input type="text" name="txtname" id="txtname" /></td>
 </tr>
 <tr>
  <td colspan="2"><input type="button" value="Submit"  onclick="ajaxFunction();" /></td>
 </tr>
</table>
<div id="message"></div>
</form>
</body>
</head>
</html>